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;
}