hdu6806 Equal Sentences // dp

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<cstdio>

#define ll long long
using namespace std;

const int maxn = 1e5 + 10;
const int m = 1e9 + 7;

int dp[maxn][2];

int main(){
    int T;cin >> T;
    while(T--)
    {
        dp[1][0] = 1;
        dp[1][1] = 0;//以第1位为结尾的串不存在与前一位的交换情况
        int n;cin >> n;
        string now, last;
        
        cin >> last;
        for(int i = 2 ; i <= n ; i++){
            cin >> now;
            if(now == last){
                dp[i][1] = 0;//如果二者相同,则换与不换无异,调换情况直接计0 
            }else{
                dp[i][1] = dp[i - 1][0];//如果本轮要换,则上一轮必须不换,数量相同 
            }
            dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) % m;//本轮不换,则对于上一轮交换情况没有要求,相加即可 
            
            last = now;
        }
        
        ll res = (dp[n][0] + dp[n][1]) % m;
        cout << res << endl;
    }
        
    return 0;
}
原文地址:https://www.cnblogs.com/ecustlegendn324/p/13460163.html