codeforces 1245 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.

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

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

Examples
inputCopy
ouuokarinn
outputCopy
4
inputCopy
banana
outputCopy
1
inputCopy
nnn
outputCopy
3
inputCopy
amanda
outputCopy
0
Note
For the first example, the candidate strings are the following: “ouuokarinn”, “ouuokarim”, “owokarim”, and “owokarinn”.

For the second example, there is only one: “banana”.

For the third example, the candidate strings are the following: “nm”, “mn” and “nnn”.

For the last example, there are no candidate strings that the machine can turn into “amanda”, since the machine won’t inscribe ‘m’.

洛谷翻译

Constanze是全村最聪明的女孩,但是她视力不好

一天,她发明了一台神奇的机器。当你念字母时,机器会把它们写刻在一张纸上。举个例子,如果你按这个顺序念“cc”、“oo”、“dd”和“ee”,那么机器就会在纸上写“codecode”。多亏了这台机器,她终于不用眼镜就能写信了

但是,她的朋友AkkoAkko决定对她开个玩笑,AkkoAkko改了机器,如果你念ww,它会写uuuu而不是ww,如果你念mm,它会写nnnn而不是mm。由于ConstanzeConstanze视力不好,她不知道AkkoAkko做了什么

其他字母和以前一样:如果你念的是“ww”,“mm”以外的字母,机器就会把它原封不动地写在纸上

第二天,我在邮箱里收到了一封信。但我看不懂,我想这应该是ConstanzeConstanze用她的机器写的。但既然我知道AkkoAkko做了什么,我就可以把ConstanzeConstanze的机器写的话变成我原本可能得到的信息。

但是因为我很菜,所以我要向你求助。你需要告诉我告诉我所有原本可能得到的信息有多少种。

由于答案可能很大,请输出答案对10^9+710
9
+7取模后的结果。

如果没有一个字符串经过ConstanzeConstanze的机器后能得到我的结果,请输出00

输入格式
一个字符串s(1\le\left|s\right|\le10^5)s(1≤∣s∣≤10
5
),表示我收到的信息,ss只包含小写字母。

输出格式
输出我所有原本可能得到的信息有多少种(即多少个字符串经过机器后能得到我收到的信息),对10^9+710
9
+7取模。

说明/提示
对于第一个样例,可能的字符串如下:“ouokarinouokarin”、“ouokarimouokarim”、“owokarimowokarim”和“owokarinowokarin”

第二个样例只有一个:“bananabanana”

对于第三个样例,可能的字符串如下:“nmnm”、“mnmn”和“nnnnnn”

在最后一个样例中,没有任何字符串可以被机器转换成“amandaamanda”,因为机器不会写下“mm”。

思路(dp)

1.设d[i]是连续u/n所构成结果的数量
2.然后找规律,其实是斐波那契数列 f[i]=f[i-1]+f[i-2];这里数据很大,要注意每一步都要取余
3.如果遇到m/w直接输出0,否则,扫一遍字符串,求出res,这里要注意计算最后遍历完字符串之后的cnt

AC代码

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int mod = 1e9 + 7;
const int N=1e5+10;

string s;
int f[N]={1,1,2,3};
int main() {

    for(int i=4;i<N;i++){
        f[i]=(f[i-1]+f[i-2])%mod;
    }

    cin >> s;
    int len=s.length();
    for(int i=0;i<len;i++){
        if (s[i]=='m'||s[i]=='w'){
            cout<<'0'<<endl;
            return 0;
        }
    }
    ll cnt=1;
    ll res=1;
    for(int i=0;i<len;i++){
        if (s[i]==s[i+1]&&(s[i]=='n'||s[i]=='u')){
            cnt++;
        }else{
            res*=f[cnt];
            res%=mod;
            cnt=1;
        }
    }
    if (cnt!=1){
        res*=f[cnt];
        res%=mod;
    }
    cout<<res<<endl;

    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值