Codeforces Round #472 _A

8644 단어 codeforces
A. Tritonic Iridescence
Overlooking the captivating blend of myriads of vernal hues, Arkady the painter lays out a long, long canvas.
Arkady has a sufficiently large amount of paint of three colours: cyan, magenta, and yellow. On the one-dimensional canvas split into n consecutive segments, each segment needs to be painted in one of the colours.
Arkady has already painted some (possibly none or all) segments and passes the paintbrush to you. You are to determine whether there are at least two ways of colouring all the unpainted segments so that no two adjacent segments are of the same colour. Two ways are considered different if and only if a segment is painted in different colours in them.
Input The first line contains a single positive integer n (1 ≤ n ≤ 100) — the length of the canvas.
The second line contains a string s of n characters, the i-th of which is either ‘C’ (denoting a segment painted in cyan), ‘M’ (denoting one painted in magenta), ‘Y’ (one painted in yellow), or ‘?’ (an unpainted one).
Output If there are at least two different ways of painting, output “Yes”; otherwise output “No” (both without quotes).
You can print each character in any case (upper or lower).
Examples inputCopy 5 CY??Y output Yes inputCopy 5 C?C?Y output Yes inputCopy 5 ?CYC? output Yes inputCopy 5 C??MM output No inputCopy 3 MMY output No Note For the first example, there are exactly two different ways of colouring: CYCMY and CYMCY.
For the second example, there are also exactly two different ways of colouring: CMCMY and CYCMY.
For the third example, there are four ways of colouring: MCYCM, MCYCY, YCYCM, and YCYCY.
For the fourth example, no matter how the unpainted segments are coloured, the existing magenta segments will prevent the painting from satisfying the requirements. The similar is true for the fifth example.
제목: 염색 문제 -> 문자열을 줍니다. Y, C, M,?,4자 Y C M 은 각각 3가지 색상을 나타냅니다. '?'Y C M 으로 염색할 수 있으며, 한 가지 색은 서로 인접한 두 문자가 같은 색일 수 없으며, 주어진 문자열이 염색에 성공할 수 있는지 판단하고, 두 가지 이상의 염색 방안이 존재한다
사고방식: 염색 방안에 점차적인 관계가 있음을 잘 발견할 수 있다.
dp[i][j]는 길이가 i이고 j 문자로 끝나는 염색 방안의 수를 나타낸다. 0은 색 Y 1이고 C는 색 M이다.
그러면 dp[i][0]=dp[i-1][1]+dp[i-1][2]dp[i][1]=dp[i-1][0]+dp[i-1][2]dp[2]=dp[i-1][0]+dp[i-1][1] 이런 문자열의 염색 방안 수량은 바로 계산할 수 있지만!!Trick이 있는데 염색 방안이 크고 롱롱롱롱하기 때문에 dp[i][j]가 2보다 크면 dp[i][j]=2로 하면 됩니다.
코드:
#include
#include
using namespace std;
typedef long long ll;
ll dp[1005][3];
char str[1005];//c,y,m

int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    cin>>str[i];


    if(str[1]=='?'){
            dp[1][0]=1;
            dp[1][1]=1;
            dp[1][2]=1;
        }
        else{
            if(str[1]=='C'){
                dp[1][0]=1;
            }
            else if(str[1]=='Y'){
                    dp[1][1]=1;
            }
            else{
                dp[1][2]=1;
            }
        }
    for(int i=2;i<=n;i++){
        if(str[i]=='?'){
            dp[i][0]=dp[i-1][1]+dp[i-1][2];
            dp[i][1]=dp[i-1][0]+dp[i-1][2];
            dp[i][2]=dp[i-1][0]+dp[i-1][1];

        }
        else{
            if(str[i]=='C'){
                dp[i][0]=dp[i-1][1]+dp[i-1][2];

            }
            else if(str[i]=='Y'){
                    dp[i][1]+=dp[i-1][0]+dp[i-1][2];
            }
            else{
                dp[i][2]=dp[i-1][0]+dp[i-1][1];
            }
        }
        if(dp[i][0]>=2)dp[i][0]=2;
        if(dp[i][1]>=2)dp[i][1]=2;
        if(dp[i][2]>=2)dp[i][2]=2;

    }
    ll ans=0;
    if(str[n]=='?'){
        ans+=dp[n][0]+dp[n][1]+dp[n][2];
    }
    else{
            if(str[n]=='C'){
                ans=dp[n][0];
            }
            else if(str[n]=='Y'){
                    ans=dp[n][1];
            }
            else{
            ans=dp[n][2];
            }
        }
        if(ans>=2)
        cout<<"Yes"<else
        cout<<"No"<return 0;
} 

좋은 웹페이지 즐겨찾기