boj 316

Description

ykwd's password is a number sequence. Every number in this sequence is no less than 0 and no larger than 255, and without leading zeros. ykwd wrote his password down ,but he didn't add spaces between numbers. For example, if his password is 1 34 8 205,he will write down 1348205 . Now, he has forgot his password, he only has the string he has written down, he wants to know how many different possible passwords he can have?

 

Input

The first line has one interger t, denoting the number of cases.
Nest t lines, each line includes a string ykwd wrote down.
You can suppose that each string is no longer than 1000.


Output

For each case you should output one line. Because the answer may be very large, you need only ouput the answer mod 1234567.


Sample Input

2
1234
1024


Sample Output

7
5

 

思路:

s:字符串

dp[n]表示长度为n的字符串可能的密码个数。

对于dp[i],j∈[0,i-1],如果s[j,i]对应的整数大小小于等于255,则dp[i]+=dp[j-1]

注意前导0,输出要mod1234567

代码:

#include<iostream>
using namespace std;

char a[1005];
int b[1005];
int dp[1005];
int pow(int m,int n)
{
	int mul=1;
	for(int i=0;i<n;i++)
		mul=mul*m;
	return mul;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",a);
		int len=strlen(a);
		dp[0]=1;
		for(int i=1;i<=len;i++)
		{
			b[i]=a[i-1]-'0';
			dp[i]=dp[i-1];
			int judge = b[i];
			for(int j=i-1;j>0;j--)
			{
				judge+=b[j]*pow(10,i-j);
				if(judge>255)
					break;
				if(b[j]!=0)
					dp[i]+=dp[j-1]%1234567;
			}
		}
		printf("%d\n",dp[len]%1234567);
	}
}

 

你可能感兴趣的:(BO)