Poor Ramzi
Ramzi is in a lot of troubles nowadays and he has a weird habit. When he starts thinking about his troubles, he takes a pen and starts writing a sequence of zeros and ones. We really don't know why!.
One day, he got sick of thinking about his problems and tried to keep himself busy by playing with the sequence he wrote. He divided the sequence into consecutive ranges, then he wrote down a new sequence consisting of the sum of digits in each range.
Once he finished, he noticed something, the sequence was palindromic. He called it a sumindrome sequence.
A palindromic sequence is a sequence that reads the same backward as forward, such as {3, 5, 7, 5, 3}. Ramzi wants to know how lucky he is, so he is interested in the number of different ways of dividing the original sequence leading to a sumindrome sequence. Ramzi doesn't like huge numbers, so he wants the answer modulo (109 + 7).
Can you help him and give him the answer?
Input
The first line contains a single integer T denoting the number of test cases.
Each test case consists of one line which contains the original sequence of zeros and ones.
The length of every sequence is less than or equal to 50.
Output
For each test print one line containing one integer, the number of different ways to divide the original sequence leading to a sumindrome sequence modulo (109 + 7).
Example
Input
2
0110
1001
Output
4
8
Note
The ways of dividing the sequence in the second sample are:
(1001) -> 2
(1)(001) -> 1, 1
(100)(1) -> 1, 1
(10)(01) -> 1, 1
(1)(00)(1) -> 1, 0, 1
(1)(0)(01) -> 1, 0, 1
(10)(0)(1) -> 1, 0, 1
(1)(0)(0)(1) -> 1, 0, 0, 1
一、原题地址
二、大致题意
看note的提示,就是分组看一组里面有几个1,然后组成的新的序列要是一个回文串。要求输出有多少种情况,需要MOD 1e7。
三、思路
要求的回文串,其实就是考虑前后的组里面1的个数。因为数据很小所以可以爆搜,l ,r表示当前搜索到的位置。需要注意的是在搜索中要加上DFS(r,r)表示将当前剩余的所有01分到一组。
四、代码
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int inf = (1 << 30) - 1;
const long long int INF = 1e18;
typedef long long LL;
const int MOD = 1e9 + 7;
int T, len;
int dp[60][60];
char s[60];
int DFS(int l, int r)
{
if (l >= r)return 1;
if (dp[l][r] != -1)return dp[l][r];
int &ret = dp[l][r];
ret = 0;
int lsum = 0;
for (int i = l; i < r; i++)
{
lsum += (s[i] - '0');
int rsum = 0;
for (int j = r; j > i; j--)
{
rsum += (s[j] - '0');
if (rsum > lsum)break;
if (lsum == rsum)
ret += DFS(i + 1, j - 1),ret%=MOD;
}
}
ret += DFS(r, r);//表示将当前剩余的所有01分到一组
return ret%MOD;
}
int main()
{
scanf("%d", &T);
while (T--)
{
memset(dp, -1, sizeof(dp));
scanf("%s", &s);
len = strlen(s);
printf("%d\n", DFS(0, len - 1));
}
getchar();
getchar();
}