A string is called sstring if it consists of lowercase english letters and no two of its consecutive characters are the same.
You are given string s of length n. Calculate the number of sstrings of length that are not lexicographically greater than s.
Input format
The only line of input contains the string s. It's length is not greater than 100.
All characters of input are lowercase english letters.
Output format:
Print the answer of test modulo 1009 to the only line of output.
Sample input:
bcd
Sample output:
653
You are given string s of length n. Calculate the number of sstrings of length that are not lexicographically greater than s.
Input format
The only line of input contains the string s. It's length is not greater than 100.
All characters of input are lowercase english letters.
Output format:
Print the answer of test modulo 1009 to the only line of output.
Sample input:
bcd
Sample output:
653
题目来自
http://www.careercup.com/question?id=23869663
得票最高的解决方案是递归,改成非递归。
懒得了解stackoverflow跟哪里
还是在这里吐槽下
面fb,好紧张啊><
希望别被刷
int main() {
string str;
cin>>str;
int len;
int MOD=1009;
int muls[102];
len = str.length();
muls[0] = 1;
for (int i = 1; i <= len; i++)
muls[i] = (muls[i-1] * 25) % MOD;
int sum=0;
char prev=' ';
for (int i=0;i<len;++i)
{
for (char c='a';c<str[i];++c)
{
if(c==prev)continue;
sum=(sum+muls[len-1-i])%MOD;
}
if(prev==str[i])break;
prev=str[i];
}
sum+=1;//same
printf("%d\n", sum); // 26 is a non-existing letter before the start
return 0;
}
本文介绍了一个算法问题,即计算不超过给定字符串字典序的所有特定形式的字符串数量,并提供了一个非递归解决方案,该方案使用了动态规划的思想来解决这个问题。

4102

被折叠的 条评论
为什么被折叠?



