D Password
| Accept:25 |
Submit:68 |
| Time Limit:1000MS |
Memory Limit:65536KB |
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
submit Discuss
/*簡單的動態規劃。。 */ #include<iostream>#include<cstdio>#include<algorithm>using namespace std;#define manx 1009long long dp[manx];int main(){ int t; cin>>t; while(t--){ memset(dp,0,sizeof(dp)); string s; cin>>s; if(s.size()>=1) dp[0]=1; if(s.size()>=2){ int sum = s[1]-'0'; sum += (s[0]-'0')*10; if(sum>=10) dp[1]=2; else dp[1]=1; } if(s.size()>=3){ int sum = s[2]-'0'; sum += (s[1]-'0')*10; dp[2]+=dp[1]; if(sum>=10) dp[2]+=1; sum += (s[0]-'0')*100; if(sum >= 100 && sum<=255) dp[2]+=1; } for(int i=3;i<s.size();i++){ int sum = s[i]-'0'; dp[i]+=dp[i-1]; dp[i]%=1234567; sum = sum + (s[i-1]-'0')*10; if(sum >= 10) dp[i]+=dp[i-2]; dp[i]%=1234567; sum = sum + (s[i-2]-'0')*100; if(sum >= 100 && sum<=255) dp[i]+=dp[i-3]; dp[i]%=1234567; } cout<<dp[s.size()-1]<<endl; }}