题目描述
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
input1
ouuokarinn
output1
4
input2
banana
output2
1
input3
nnn
output3
3
input4
amanda
output4
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很聪明,但是视力不好。她发明了一种机器,可以在她读单词的时候将单词记录下来。Akko搞了个恶作剧,该机器会将所有的‘m’ 变为‘nn’,将’w’, 变成‘uu’.一天,我收到了一封信(一个字符串)来自Constanze,但是我看不懂,问这个字符串原来的形式有几种可能。
如果字符串出现‘m’ 和‘w’, 则输出‘0’;
题解:
我们可以发现(以‘m’, ‘nn’ 为例,另一个同理)
当有2个连续的 ‘n’
共有两种情况:‘nn’, ‘m’
当有3个连续的 ‘n’
共有三种情况: ‘nnn’, ‘nm’, ‘mn’
当有4个连续的 ‘n’
共有五种情况: ‘nnnn’, ‘nnm’, ;nmn’, ‘mnn’, ‘mm’
再考虑 w个连续的‘n’, 记为f(w)
分两种情况
(1) 前w - 2 个连续‘n’ 的种数,再加个‘m’,f(w - 2);
(2) 前w - 1 个连续‘n’ 的种数,再加个‘n’, f(w - 1);
故 f(w) = f(w - 1) + f(w - 2)
这不就是Fibonacci数列吗
最后,我们就可以将所有含两个或两个以上的‘n’, 或‘u’的种数相乘 (利用乘法原理)
代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
const int N = 100005;
const int mod = 1e9 + 7;
long long fib[N];
string a;
long long mul(long long a, int b){ //快速乘,用于取模
a %= mod;
long long ans = 0;
while(b){
if(b % 2) ans = (ans + a) % mod;
b >>= 1;
a = a + a;
if(a >= mod) a -= mod;
}
return ans;
}
int main(){
int i;
fib[0] = 1;
fib[1] = 1;
for(i = 2; i <= N - 4; i++){ //预处理Fibonacci数列
fib[i] = fib[i - 1] + fib[i - 2];
if(fib[i] >= mod){
fib[i] -= mod;
}
}
cin >> a;
int len = a.size();
int cnt1 = 0, cnt2 = 0;
long long ans = 1;
for(i = 0; i < len; i++){ //检验是否合法
if(a[i] == 'm'){
printf("0\n");
return 0;
}
if(a[i] == 'w'){
printf("0\n");
return 0;
}
}
for(i = 0; i < len; i++){
if(a[i] != 'n' && cnt1 > 0){
ans = mul(ans, fib[cnt1]);
cnt1 = 0;
}
if(a[i] != 'u' && cnt2 > 0){
ans = mul(ans, fib[cnt2]);
cnt2 = 0;
}
if(a[i] == 'n'){
cnt1++;
}
if(a[i] == 'u'){
cnt2++;
}
}
if(cnt1 > 0){
ans = mul(ans, fib[cnt1]);
}
if(cnt2 > 0){
ans = mul(ans, fib[cnt2]);
}
cout << ans << "\n";
return 0;
}