codeforces#597 C. Constanze's Machine(简单dp)

codeforces#597 C. Constanze's Machine(简单dp)_第1张图片

codeforces#597 C. Constanze's Machine(简单dp)_第2张图片

codeforces#597 C. Constanze's Machine(简单dp)_第3张图片

题意:给定一个字符串,如果在该字符串中存在m或w时,输出0,否则,求存在u和n的字符串有多少种方案数。

思路:

#include

using namespace std;
typedef long long LL;

const int maxn = 1e5+5;
const int mod = 1e9+7;
char s[maxn];
LL dp[maxn];

int main()
{
	scanf("%s", s);
	int len = strlen(s);
	
	for(int i = 0; i < len; i++){
		if(s[i] == 'm' || s[i] == 'w'){
			printf("0\n");
			return 0;
		}
	}
	dp[0] = dp[1] = 1;
	for(int i = 1; i < len; i++){
		if(s[i] == s[i-1]){
			if(s[i] == 'u' || s[i] == 'n')
				dp[i+1] = (dp[i] + dp[i-1]) % mod;
			else dp[i+1] = dp[i];
		}
		else dp[i+1] = dp[i];
	}
	printf("%lld\n", dp[len]);
	return 0;
}

 

你可能感兴趣的:(dp,动态规划)