首页 > 试题广场 >

Ternary String

[编程题]Ternary String
A ternary string is a sequence of digits, where each digit is either 0, 1, or 2.
Chiaki has a ternary string s which can self-reproduce. Every second, a digit 0 is inserted after every 1 in the string, and then a digit 1 is inserted after every 2 in the string, and finally the first character will disappear.
For example, ``212'' will become ``11021'' after one second, and become ``01002110'' after another second.
Chiaki would like to know the number of seconds needed until the string become an empty string. As the answer could be very large, she only needs the answer modulo (109 + 7).

输入描述:
There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:
The first line contains a ternary string s (1 ≤ |s| ≤ 105).
It is guaranteed that the sum of all |s| does not exceed 2 x 106.


输出描述:
For each test case, output an integer denoting the answer. If the string never becomes empty, output -1 instead.
示例1

输入

3
000
012
22

输出

3
93
45
头像 早晚
发表于 2021-10-04 19:13:39
D题偶然发现,这个奇数不行,偶数可以,并且都能从上一个可以的情况推出来,可以把答案从n=2开始,然后上面放1下边放-1,然后再调两边,最后看成两个三角形,以对角线为分界线,上面全是1,下面全是-1,对角线上,前n/2行是1,后是0. #include <iostream> #includ 展开全文