C. Constanze's Machine dp水题

题目:http://codeforces.com/contest/1245/problem/C

C. Constanze's Machine

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Constanze is the smartest girl in her village but she has bad eyesight.

One day, she was able to invent an incredible machine! When you pronounce letters, the machine will inscribe them onto a piece of paper. For example, if you pronounce 'c', 'o', 'd', and 'e' in that order, then the machine will inscribe "code" onto the paper. Thanks to this machine, she can finally write messages without using her glasses.

However, her dumb friend Akko decided to play a prank on her. Akko tinkered with the machine so that if you pronounce 'w', it will inscribe "uu" instead of "w", and if you pronounce 'm', it will inscribe "nn" instead of "m"! Since Constanze had bad eyesight, she was not able to realize what Akko did.

The rest of the letters behave the same as before: if you pronounce any letter besides 'w' and 'm', the machine will just inscribe it onto a piece of paper.

The next day, I received a letter in my mailbox. I can't understand it so I think it's either just some gibberish from Akko, or Constanze made it using her machine. But since I know what Akko did, I can just list down all possible strings that Constanze's machine would have turned into the message I got and see if anything makes sense.

But I need to know how much paper I will need, and that's why I'm asking you for help. Tell me the number of strings that Constanze's machine would've turned into the message I got.

But since this number can be quite large, tell me instead its remainder when divided by 109+7.

If there are no strings that Constanze's machine would've turned into the message I got, then print 0

Input

Input consists of a single line containing a string ss (1≤|s|≤105) — the received message. ss contains only lowercase Latin letters.

Output

Print a single integer — the number of strings that Constanze's machine would've turned into the message ss, modulo 109+7.

题意:  找出有多少字符串的方案, nn可以变成m也可以不变,uu可以变成w也可以不变,如果出现w或m输出0;

dp记录每个前状态即可

ac代码:

#include <cstdio>
#include <cstring>
#define inf 0x3f3f3f3f
const int mx = 2e5+100;
typedef long long ll;
const ll mo = 1e9+7;
char s[mx];
ll dp[mx];

int main () {
    scanf("%s", s+1);
    dp[0] = 1;
    dp[1] = 1;
    int i;
    bool f = 0;
    for (i = 2; s[i]; ++i) {
        if (s[i] == s[i-1] && (s[i] == 'u' || s[i] == 'n'))
            dp[i] = (dp[i-1]+dp[i-2])%mo;
        else dp[i] = dp[i-1];
        if (s[i] == 'm' || s[i] == 'w') f = 1;
    }
    if (f || s[1] == 'm' || s[1] == 'w') puts("0");
    else
        printf("%lld\n", dp[i-1]);
    return 0;
}
发布了74 篇原创文章 · 获赞 29 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/ClonH/article/details/102897688
今日推荐