Codeforces Round #604 (Div. 2) A. Beautiful String

Posted ydddd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #604 (Div. 2) A. Beautiful String相关的知识,希望对你有一定的参考价值。

链接:

https://codeforces.com/contest/1265/problem/A

题意:

A string is called beautiful if no two consecutive characters are equal. For example, "ababcb", "a" and "abab" are beautiful strings, while "aaaaaa", "abaa" and "bb" are not.

Ahcl wants to construct a beautiful string. He has a string s, consisting of only characters ‘a‘, ‘b‘, ‘c‘ and ‘?‘. Ahcl needs to replace each character ‘?‘ with one of the three characters ‘a‘, ‘b‘ or ‘c‘, such that the resulting string is beautiful. Please help him!

More formally, after replacing all characters ‘?‘, the condition si≠si+1 should be satisfied for all 1≤i≤|s|?1, where |s| is the length of the string s.

思路:

直接暴力枚举,判断一遍

代码:

#include<bits/stdc++.h>
using namespace std;
 
string s;
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        cin >> s;
        bool flag = true;
        for (int i = 0;i < s.length();i++)
        {
            if (s[i] == '?')
            {
                int t = -1;
                for (int j = 0;j < 3;j++)
                {
                    if ((i-1 < 0 || s[i-1] != (char)('a'+j)) && (i+1 >= s.length() || s[i+1] != (char)('a'+j)))
                    {
                        t = j;
                        break;
                    }
                }
                s[i] = (char)('a'+t);
            }
        }
        for (int i = 1;i < s.length();i++)
        {
            if (s[i] == s[i-1])
                flag = false;
        }
        cout << (flag ? s : "-1") << endl;
    }
 
    return 0;
}

以上是关于Codeforces Round #604 (Div. 2) A. Beautiful String的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #604 (Div. 2)

Codeforces Round #604 (Div. 2)

Codeforces Round #604 (Div. 2)

Codeforces Round #604 (Div. 2) A. Beautiful String

Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)

Codeforces Round #604 (Div. 2) C. Beautiful Regional Contest