编程介的小学生 2020-01-17 21:48 采纳率: 20.5%
浏览 81

Moles 的算法问题

Problem Description
A mole is a strange mammal adapted to a subterranean lifestyle. It has invisible eyes and short, powerful limbs with large paws oriented for digging.

Before the catastrophic earthquake, a group of moles have perceived the disaster. They move to a safe place and want to dig holes under the ground. There are n moles, numbered from 1 to n. They stand in a line and dig holes one by one. The mole line is not necessary sorted by the mole number. Every mole should live in a hole dug by itself and every mole just digs one hole. The first mole in the line digs the first hole with a channel to the ground. Then other moles go down through that channel and dig more holes and channels. A hole may have at most three neighbors connected by channels, one is on the upper level, and the other two holes are on the lower level lying on the left side and the right side. When a mole reaches a hole, if its number is smaller than the hole owner's, it will go to the left-lower hole(or digs a left-lower hole and stays there when there is no left-lower hole), otherwise it will go to the right-lower hole(or digs a right-lower hole and stays there when there is no right-lower hole). Due to the excellent ability and well-designed layout, these holes and channels will not cross with each other.

Mouse Jerry is a friend of those moles. He comes to visit them and prepares gifts for every mole. There is a rule that the mole whose number is smaller must get the gift earlier than the mole whose number is larger. Jerry starts from the ground. He travels through the holes and channels to deliver his gifts. After giving out all his gifts, he comes back to the ground. In the mole world, it is interesting that the moles with odd numbers are males and others are females. When reaching a hole, Jerry takes a note about the gender of the hole owner(0 represents female and 1 represents male). When he gets back to the ground, he will get a 0-1 sequence. Now he wants to calculate the "harmony value". The harmony value represents the number of occurrences of a given "harmony string" in the above mentioned sequence. Occurrences may overlap.

Please note that Jerry is very smart so his travel distance is as small as possible.

Input
The first line contains an integer t meaning that there are t test cases(t <= 10).
For each test case :
The first line is an integer n meaning that there are n moles. (n <= 600000).
The second line contains n integers representing the mole line. Each integer is a mole's number and the first integer is the number of the first mole in the line.
The third line is the harmony string. The string length is not large than 7000.

Output
For each test case, please output the case number and the harmony value.

Sample Input
2
8
5 1 3 2 7 8 4 6
01
10
1 2 3 4 5 6 7 8 9 10
1010

Sample Output
Case #1: 4
Case #2: 8

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?