A string is called sstring

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


题目来自

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值