zoukankan      html  css  js  c++  java
  • Codeforces Round #597 (Div. 2) C. Constanze's Machine

    链接:

    https://codeforces.com/contest/1245/problem/C

    题意:

    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.

    思路:

    DP, 考虑连续两个转换的值,为Dp[i] = Dp[i-1]+Dp[i-2],否则Dp[i] = Dp[i-1]。
    出现w和m时为0.

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int MOD = 1e9+7;
    const int MAXN = 1e5+10;
     
    string s;
    LL Dp[MAXN];
     
    int main()
    {
        ios::sync_with_stdio(false);
        cin >> s;
        int len = s.length();
        Dp[0] = Dp[1] = 1;
        for (int i = 1;i < len;i++)
        {
            if (s[i] == s[i-1] && (s[i] == 'u' || s[i] == 'n'))
                Dp[i+1] = (Dp[i]+Dp[i-1])%MOD;
            else
                Dp[i+1] = Dp[i];
        }
        for (int i = 0;i < len;i++)
        {
            if (s[i] == 'w' || s[i] == 'm')
            {
                cout << 0 << endl;
                return 0;
            }
        }
        cout << Dp[len]%MOD << endl;
     
        return 0;
    }
    
  • 相关阅读:
    DB2数据常用指令
    HTMLParser使用
    面试知识点总结之数据库
    面试知识点总结之算法
    面试知识点总结之操作系统
    面试知识点总结之计算机网络
    面试知识点总结之Java语言
    一个精确匹配的问题
    【转】矩阵求导,矩阵计算
    【转】用C语言扩展Python的功能
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11797728.html
Copyright © 2011-2022 走看看