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

    C. Constanze's Machine

    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

    input
    ouuokarinn
    output
    4
    input
    banana
    output
    1
    input
    nnn
    output
    3
    input
    amanda
    output
    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'.

    题意

    现在有个机器,会把w翻译成uu,把m翻译成nn。

    现在给你一个字符串,让你还原成原来的样子,问你有多少种还原方式。

    另外:如果字符串种存在w或者m,输出-1

    题解

    dp,两种转移,保持不变和变w/变m,转移即可。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int mod = 1e9+7;
    const int maxn = 1e5+7;
    string s;
    int dp[maxn];
    int flag = 0;
    int main(){
    	cin>>s;
    	int flag = 0;
    	dp[0]=1;
    	if(s[0]=='m'||s[0]=='w')flag = 1;
    	for(int i=1;i<s.size();i++){
    		dp[i]=dp[i-1];
    		if(s[i]=='m'||s[i]=='w')flag = 1;
    		if((s[i]=='u'&&s[i-1]=='u')||(s[i]=='n'&&s[i-1]=='n')){
    			if(i==1){
    				dp[i]=(dp[i]+1)%mod;
    			}else{
    				dp[i]=(dp[i]+dp[i-2])%mod;
    			}
    		}
    	}
    	if(flag==1){
    		cout<<"0"<<endl;
    	}else{
    		cout<<dp[s.size()-1]%mod<<endl;
    	}
    }
  • 相关阅读:
    virturalbox安装CentOS桥接网卡
    解决:Error:java: 无效的源发行版: 12
    maven下载失败,镜像配置,idea的maven下载配置
    linux压缩和解压缩命令
    Unable to allocate 130176KB bitmaps for parallel garbage collection for the requested 4165632KB heap.
    Docker,Linux,Kubernetes,postgres常用的命令行(持续更新)
    dropwizard问题记录1:如何进行mvn package打包,如何在项目目录下运行
    线程的并发工具类
    学信网改绑手机号码,但是忘记了老号码怎么办?利用node.js + puppeteer 跑脚本实现改绑手机号
    javascript事件循环与js执行机制
  • 原文地址:https://www.cnblogs.com/qscqesze/p/11784529.html
Copyright © 2011-2022 走看看