Codeforces Round #657 (Div. 2) A. Acacius and String(暴力)

Acacius is studying strings theory. Today he came with the following problem.

You are given a string ss of length nn consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once?

Each question mark should be replaced with exactly one lowercase English letter. For example, string "a?b?c" can be transformed into strings "aabbc" and "azbzc", but can't be transformed into strings "aabc", "a?bbc" and "babbc".

Occurrence of a string tt of length mm in the string ss of length nn as a substring is a index ii (1≤inm+11≤i≤n−m+1 ) such that string s[i..i+m−1]s[i..i+m−1] consisting of mm consecutive symbols of ss starting from ii -th equals to string tt . For example string "ababa" has two occurrences of a string "aba" as a substring with i=1i=1 and i=3i=3 , but there are no occurrences of a string "aba" in the string "acba" as a substring.

Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once.

Input

First line of input contains an integer TT (1≤T≤50001≤T≤5000 ), number of test cases. TT pairs of lines with test case descriptions follow.

The first line of a test case description contains a single integer nn (7≤n≤507≤n≤50 ), length of a string ss .

The second line of a test case description contains string ss of length nn consisting of lowercase English letters and question marks.

Output

For each test case output an answer for it.

In case if there is no way to replace question marks in string ss with a lowercase English letters in such a way that there is exactly one occurrence of a string "abacaba" in the resulting string as a substring output "No".

Otherwise output "Yes" and in the next line output a resulting string consisting of nn lowercase English letters. If there are multiple possible strings, output any.

You may print every letter in "Yes" and "No" in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).

首先要确定原串中abacaba出现的次数:出现两次及以上输出NO;出现一次的话首先把有问号的地方随便填上z之类的字母然后输出YES和新串。如果没有出现过abacaba,就枚举每个可能的位置,看看这个位置往后的7个字母,能否通过补全?变成abacaba,如果能的话,还要注意这样填是否会造成多余的abacaba的出现,比如???????bacaba以及???c???caba这样的例子,如果会的话恢复原状继续遍历。

代码写的有点挫==仅供参考。

#include <bits/stdc++.h>
using namespace std;
char s[66];
int n;
int match(int x)
{
    string temp = "abacaba";
    int cnt1 = 0, cnt2 = 0;
    for(int i = x; i <= x + 6; i++)
    {
        if(s[i] == temp[i - x]) cnt1++;
        if(s[i] == temp[i - x] || s[i] == '?') cnt2++;
    }
    if(cnt1 == 7) return 2;//完全匹配 
    else if(cnt1 != 7 && cnt2 == 7) return  1;//可以通过补充?的字母来匹配 
    else return 0;//无法匹配 
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cin >> n;
        scanf("%s", s);
        bool ok = 0;
        int cnt = 0; 
        for(int i = 0; i < n - 7 + 1; i++)
            if(match(i) == 2) cnt++;
        if(cnt > 1)
        {
            cout << "NO" << endl;
            continue;
        }
        else if(cnt == 1)
        {
            for(int i = 0; i < n; i++) if(s[i] == '?') s[i] = 'z';
            cout << "YES" << endl;
            printf("%s
", s);
            continue;
        }
        else//有可能通过补充来匹配 
        {
            for(int i = 0; i < n - 7 + 1; i++)
            {
                if(match(i) == 1)
                {
                    string temp = "abacaba";
                    vector<char> v;
                    for(int j = i; j <= i + 6; j++) 
                    {
                        v.push_back(s[j]);
                        s[j] = temp[j - i];
                    }
                    bool no = 0;
                    for(int k = max(0, i - 6); k <= min(n - 1, i + 12); k++)//检查这样填是否会造成两个以上abacaba出现 
                    {
                        if(match(k) == 2 && k != i)//排除原位置 还有abacaba 
                        {
                            no = 1;
                            break;
                        }
                    }
                    if(no)//i这个位置不可行 
                    {
                        for(int j = i; j <= i + 6; j++) s[j] = v[j - i];
                        continue;
                    }
                    for(int i = 0; i < n; i++) if(s[i] == '?') s[i] = 'z';
                    ok = 1;
                    cout << "YES" << endl;
                    printf("%s
", s);
                    break;
                }
            }
            if(!ok) cout << "NO" << endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lipoicyclic/p/13341242.html