Codeforces 1245C Constanze's Machine

思路:

1.设dp[i]是连续的iun最多能还原成的字符串个数;
2.观察相邻几个i之间的关系,不难得出递推式:dp[i+2]=dp[i+1]+dp[i] (i>=2)
3.字符串里不能出现wm,出现了输出0;
4.最后所有情况相乘,记得中间每一步取余防止溢出;

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX_N=100005;
const LL MOD=(LL)1e9+7;
LL dp[MAX_N]={1,1,2,3};//dp[2]=2,dp[3]=3
int main(){
	for(int i=4;i<MAX_N;i++) dp[i]=(dp[i-1]+dp[i-2])%MOD;
	string s;
	cin>>s;
	for(char c:s){
		if(c=='w'||c=='m'){
			putchar('0');
			return 0;
		}
	}
	LL cnt=1,ans=1;
	for(int i=1;i<s.length();i++){
		if(s[i]==s[i-1]&&(s[i]=='u'||s[i]=='n')) cnt++;
		else{
			ans*=dp[cnt];
			ans%=MOD;
			cnt=1;
		}
	}
	if(cnt!=1) ans*=dp[cnt];
	cout<<(ans%MOD);
	return 0;
}
原文地址:https://www.cnblogs.com/yuhan-blog/p/12308879.html