Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
Statistical Charts
Submit Problem
Online Status
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Time Limit: 3000MSMemory Limit: 65536K
Total Submissions: 737Accepted: 101


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.


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.


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

Sample Input

5 1 3 2 7 8 4 6
1 2 3 4 5 6 7 8 9 10

Sample Output

Case #1: 4
Case #2: 8


In the first test case, the 0-1 sequence of the first case is “111010111101011”, and the number of occurrences of harmony string “01” is 4.


[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator