AtCoder Beginner Contest 071 D

Problem Statement

We have a board with a 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

Input is given from Standard Input in the following format:

N
S1
S2

Output

Print the number of such ways to paint the dominoes, modulo 1000000007.


Sample Input 1

Copy
3
aab
ccb

Sample Output 1

Copy
6

There are six ways as shown below:


Sample Input 2

Copy
1
Z
Z

Sample Output 2

Copy
3

Note that it is not always necessary to use all the colors.


Sample Input 3

Copy
52
RvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHn
RLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn

Sample Output 3

Copy
958681902

题解:
只有两行,简单题哈,就不多解释了.
定义F[i]为前i列的方案数
我们可以先压缩一下,s[i]=s[i+1]的缩成一个
可以开始讨论:
如果s1[i]==s2[i] & s1[i-1]==s2[i-1] F[i]=F[i-1]*2 i-1固定后,i有两种方案对应
s1[i]==s2[i] & s1[i-1]!= s2[i-1] F[i]=F[i-1] 表示i这个位置和i-1一一对应
s1[i]!= s2[i] & s1[i-1]==s2[i-1] F[i]=F[i-1]*2
s1[i]!= s2[i] & s1[i-1]!= s2[i-1] F[i]=F[i-1]*3 都不相同时有三种方案对应,可以手画下
 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 using namespace std;
 8 const int N=55,mod=1000000007;
 9 char s1[N],s2[N];long long f[N];
10 void work()
11 {
12     int l,n=0;
13     scanf("%d",&l);
14     scanf("%s",s1+1);
15     scanf("%s",s2+1);
16     for(int i=1;i<=l;i++){
17         if(s1[i]!=s1[i+1]){
18             s1[++n]=s1[i];
19             s2[n]=s2[i];
20         }
21     }
22     if(s1[1]==s2[1])f[1]=3;
23     else f[1]=6;
24     for(int i=2;i<=n;i++){
25         if(s1[i]==s2[i]){
26             if(s1[i-1]!=s2[i-1])f[i]+=f[i-1];
27             else f[i]+=(f[i-1]+f[i-1])%mod;
28         }
29         else{
30             if(s1[i-1]==s2[i-1])f[i]+=(f[i-1]+f[i-1])%mod;
31             else f[i]+=(f[i-1]*3)%mod;
32         }
33         f[i]%=mod;
34     }
35     printf("%lld
",f[n]);
36 }
37 
38 int main()
39 {
40     work();
41     return 0;
42 }


 
原文地址:https://www.cnblogs.com/Yuzao/p/7401408.html