题目描述
We have a board with a 2×N grid. Snuke covered the board with N dominoes without overlaps. Here, a domino can cover a 1×2 or 2×1 square.
Then, Snuke decided to paint these dominoes using three colors: red, cyan and green. Two dominoes that are adjacent by side should be painted by different colors. Here, it is not always necessary to use all three colors.
Find the number of such ways to paint the dominoes, modulo 1000000007.
The arrangement of the dominoes is given to you as two strings S1 and S2 in the following manner:
Each domino is represented by a different English letter (lowercase or uppercase).
The j-th character in Si represents the domino that occupies the square at the i-th row from the top and j-th column from the left.
Constraints
1≤N≤52
|S1|=|S2|=N
S1 and S2 consist of lowercase and uppercase English letters.
S1 and S2 represent a valid arrangement of dominoes.
输入
Input is given from Standard Input in the following format:
N
S1
S2
输出
Print the number of such ways to paint the dominoes, modulo 1000000007.
样例输入
3 aab ccb
样例输出
6
提示
There are six ways as shown below:
初始情况为:
一个竖着的方块共有3中可能
两个横着的木块共有6种可能
剩下的排列题主要讨论2种情况
1:前边方块为竖着的时候看将要放的木块是一个竖着的(前边的结果*2)还是两个横着的(前边的结果*1)
2:前边的方块为两个横着的时候看要放的木块是一个竖着的(前边的结果*1)还是两个横着的(前边的结果*3)
ac代码:
#include<iostream>
#include<string>
using namespace std;
const int mod=1e9+7;
int main(){
string str1,str2;
int n;
cin>>n;
cin>>str1;
cin>>str2;
int i=0;
int p;
long long int sum=1;
while(i<n){
if(i==0){
if(str1[i]==str2[i]){
sum=sum*3%mod;
p=1;
i++;
}
else{
sum=sum*6%mod;
p=2;
i+=2;
}
}
else{
if(str1[i]==str2[i]){
if(p==1){
sum=sum*2%mod;
i++;
}
else{
sum=sum*1%mod;
i++;
p=1;
}
}
else{
if(p==1){
sum=sum*2%mod;
i+=2;
p=2;
}
else{
sum=sum*3%mod;
i+=2;
}
}
}
}
cout<<sum<<endl;
return 0;
}