美文网首页C++C语言数据结构和算法分析
Codeforces Round 472-2A题解报告

Codeforces Round 472-2A题解报告

作者: 海天一树X | 来源:发表于2018-03-26 19:21 被阅读8次

    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

    input
    5
    CY??Y
    output
    Yes
    
    input
    5
    C?C?Y
    output
    Yes
    
    input
    5
    ?CYC?
    output
    Yes
    
    input
    5
    C??MM
    output
    No
    
    input
    3
    MMY
    output
    No
    

    分析

    如果所有的问号处,有两种或两种以上的颜色方案,使得任意两个相邻的颜色不一样,结果为”Yes”。否则为”No“

    可以枚举以下几种场景:
    (1)只要出现相邻两个颜色一样的,结果为”No”
    在没有相邻两个颜色一样的前提下:
    (2)第一个字符若是为’?’,则这个问号至少可以用两种颜色替换,结果为”Yes”。比如”?MC”可以替换为”YMC”或“CMC”
    (3)最后一个字符若是为’?’,则这个问号至少可以用两种颜色替换,结果为”Yes“
    (4)若出现连续两个’?’,结果为”Yes”。比如”C??M”可以替换为”CYCM”和”CMCM”
    (5)若’?’的左右字符一样,结果为”Yes”。比如”C?C”可以替换为”CMC”和”CYC”

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    void NO()
    {
        puts("No");
        exit(0);
    }
    
    void YES()
    {
        puts("Yes");
        exit(0);
    }
    
    int main()
    {
        int n;
        cin >> n;
        string s;
        cin >> s;
        for(int i = 0; i < n - 1; ++i)
        {
            if(s[i] != '?' && s[i] == s[i+1])
            {
                NO();
            }
        }
    
        for(int i = 0; i < n; ++i)
        {
            if(s[i] == '?')
            {
                if(i == 0 || i == n - 1)
                {
                    YES();
                }
    
                if(s[i+1] == '?')
                {
                    YES();
                }
    
                if(s[i-1] == s[i+1])
                {
                    YES();
                }
            }
        }
    
        NO();
    }
    

    Codeforces & TopCoder QQ交流群:648202993
    更多内容请关注微信公众号


    feicuisenlin_12x12.jpg

    相关文章

      网友评论

        本文标题:Codeforces Round 472-2A题解报告

        本文链接:https://www.haomeiwen.com/subject/bhxdcftx.html