CodeForces-1245C Constanze's Machine[斐波那契] or [dp]



Constanze’s Machine


题目传送门:CodeForces-1245C

Time Limit 1000 ms
Memory limit 262144 kB



Problem Description:

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 1 0 9 + 7 10^9+7 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|≤ 1 0 5 10^5 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 1 0 9 + 7 10^9+7 109+7.


Sample:

Sample OneInput:

ouuokarinn

Sample OneOutput:

ouuokarinn

Sample TwoInput:

banana

Sample TwoOutput:

1

Sample ThreeInput:

nnn

Sample ThreeOutput:

3

Sample FourInput:

amanda

Sample FourOutput:

0



题目大意:

她愚蠢的朋友Akko决定对她开玩笑。 Akko对机器进行了修补,因此,如果您发音为“ w”,则将题为“ uu”而不是“ w”;如果发音为“ m”,则将为题为“ nn”而不是“ m”!由于Constanze视力不好,她无法意识到Akko的所作所为。
其余字母的行为与以前相同:如果您发音除“ w”和“ m”之外的任何字母,则机器将其刻在一张纸上。
也就是说先给你一个字符串,但这个字符串不能含有 " w " "w" "w" " m " "m" "m",如果含有则输出0
且这个字符串中的连续两个 " n " "n" "n"可以合成一个 " m " "m" "m",连续两个 " u " "u" "u"可以合成一个 " w " "w" "w",问有多少种组合的可能性


思路:

没想到是斐波那契数列,好像打个表可以看出来(考完试再看下)
看了第一眼下意识的感觉是 d p dp dp就直接写 d p dp dp,难得自己写 d p dp dp可以一次性过
可以考虑第i个位置选择合并或者不合并的两种可能性的方案数相加
如果有连续的两个 " n " "n" "n"或者 " u " "u" "u",选择不合并,到当前为止的总方案数 d p [ i ] = d p [ i − 1 ] dp[i] = dp[i-1] dp[i]=dp[i1]
如果选择合并,当目前为止的总方案数就是 d p [ i ] = d p [ i − 2 ] dp[i] = dp[i-2] dp[i]=dp[i2],因为这两个进行合并,所以就等于 i − 2 i-2 i2的位置的方案数
因此如果有连续的两个 " n " "n" "n"或者 " u " "u" "u",那么 d p [ i ] = d p [ i − 1 ] + d p [ i − 2 ] dp[i] = dp[i-1] + dp[i-2] dp[i]=dp[i1]+dp[i2]
如果没有则直接 d p [ i ] = d p [ i − 1 ] dp[i] = dp[i-1] dp[i]=dp[i1]



Code:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define  endc       std::ios::sync_with_stdio(false); // 关掉c++流
#define  INOPEN     freopen("in.txt", "r", stdin)
#define  OUTOPEN    freopen("out.txt", "w", stdout)
#define  mes(a, b)  memset(a, b, sizeof a)
#define  qwq(i, j)  for(int i = 0; i < j; ++i)
#define  qeq(i, j)  for(int i = 1; i <= j; ++i)
#define  isdigit(a) ((a)>='0'&&(a)<='9')
#define  xiao(a)    ((a)>='a'&&(a)<='z')
#define  da(a)      ((a)>='A'&&(a)<='Z')
#define  pii        pair
#define  lowbit(x)  x & (-x)
#define  fi         first
#define  se         second
#define  lson       id<<1
#define  rson       id<<1|1

typedef unsigned long long int ull;
typedef long long int ll;
const double eps  = 1e-8;
const double pi   = acos(-1.0);
const int    inf  = 0x3f3f3f3f;
const ll     INF  = 1e18 + 100;
const int    maxm = 1e6 + 6;
const int    maxn = 1e5 + 10;
const int    mod  = 1e9+7;
using namespace std;


int n, m, cas, tol = 0;
int head[maxn];
struct Edge {int u, v, w;}edge[maxm];

template<typename T>void re(T &x){x = 0; int f = 0; char ch = getchar();while(!isdigit(ch)){if(ch == '-') f=1; ch=getchar();}while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0'; ch=getchar();}x = f?-x:x;}
// templateT fpow(T a, ll b) {T ans = 1; while(b) {if(b & 1) ans = a * ans % mod; a = a * a % mod; b >>= 1;}return ans;}
// inline void adde(int u, int v, int w) {edge[tol] = Edge{v, head[u], w}; head[u] = tol++;}
//POJ 3463 - Sightseeing

char str[maxn];
ll dp[maxn];

int main() {
    scanf("%s", str + 1);
    int L, flag = 0;
    L = strlen(str + 1);
    dp[0] = 1ll;
    for(int i = 1; i <= L; ++i) {
        if(str[i] == 'w' || str[i] == 'm') {
            flag = 1;
            break;
        }
        if((str[i] == 'n' && str[i-1] == 'n') || (str[i] == 'u' && str[i-1] == 'u')) {
            dp[i] = (dp[i-1] + dp[i-2]) % mod;
        }else{
            dp[i] = dp[i-1];
        }
    }
    printf("%lld\n", flag ? 0 : dp[L]);
    return 0;
}

你可能感兴趣的:(CodeForces-1245C Constanze's Machine[斐波那契] or [dp])