AtCoder Regular Contest 081 D

来源:互联网 发布:淘宝日本代购退换 编辑:程序博客网 时间:2024/06/06 14:15

D - Coloring Dominoes
Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement
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
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
题目链接
题解:就第一块是有三种情况
abb
acc 如果这种情况就乘2

bbcc
ddee如果这种情况乘3

#include <iostream>#include <stdio.h>#include <algorithm>#include <vector>#include <string.h>#include <map>#define int long longusing namespace std;const int mod=1000000007;char str[12][100];signed main(){    int n;    scanf("%lld",&n);    for(int i=0;i<2;i++){        scanf("%s",str[i]);    }    bool flag=false;    int f=0;    int t=1;    for(int i=0;i<n;i++){        if(str[0][i]==str[0][i+1]){            if(f==0){                t*=3;                t*=2;                f=1;            } else {                if(flag){                    t*=2;                    t*=1;                } else {                    t*=3;                }            }            flag=false;            i++;        } else {            if(str[0][i]==str[1][i]){                if(f==0){                    t*=3;                    f=1;                } else {                    if(flag) t*=2;                    else t*=1;                }                flag=true;            }        }        t%=mod;    }    cout<<t<<endl;    return 0;}
原创粉丝点击