Codeforces Round #597 (Div. 2) C题

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.


惨痛的cf上分之路,这道题都没整出来;

昨晚太困了,脑子都不在线;

手推一下规律,发现连续的n或u能组成的m和w的个数是一个斐波那契数列;

所以只要统计一下连续的n或u有多长就行了;然后就是算一下乘积

代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
//ios::sync_with_stdio(false);
using namespace std;
const int N=100100;
const int M=200100;
const LL mod=1e9+7;
LL dfs(LL p){
	if(p==2) return p;
	if(p==3) return p;
	LL a1=2LL,a2=3LL;
	LL s=0;
	for(LL i=4;i<=p;i++){
		s=a1+a2;
		s%=mod;
		a1=a2;
		a1%=mod;
		a2=s;
	}
	return s;
}
vector<LL>ve;
int main(){
	ios::sync_with_stdio(false);
	string s;
	cin>>s;
	LL u=1,n=1;
	if(s[0]=='m'||s[0]=='w'){//因为从1开始循环,所以要特判一下0 
		cout<<0<<endl;
		return 0; 
	}
	for(int i=1;i<s.length();i++){
		if(s[i]=='m'||s[i]=='w'){
			cout<<0<<endl;
			return 0; 
		}
		if(s[i]=='u'&&s[i-1]=='u'){
			u++;
		}
		else if(u>1){
			ve.push_back(dfs(u));
			u=1;
		}
		if(s[i]=='n'&&s[i-1]=='n'){
			n++;
		}
		else if(n>1){
			ve.push_back(dfs(n));
			n=1;
		}
	}
	if(n>1) ve.push_back(dfs(n));
	if(u>1) ve.push_back(dfs(u));
	LL sum=1LL;
	for(int i=0;i<ve.size();i++){
		sum=sum*ve[i];
		sum%=mod;
	}
	cout<<sum<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值