1. 程式人生 > 實用技巧 >hdu6806 Equal Sentences // dp

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; }