A. Acacius and String


time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
Acacius is studying strings theory. Today he came with the following problem.

You are given a string s of length n 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 t of length m in the string s of length n as a substring is a index i (1≤i≤n−m+1) such that string s[i…i+m−1] consisting of m consecutive symbols of s starting from i-th equals to string t. For example string “ababa” has two occurrences of a string “aba” as a substring with i=1 and i=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 T (1≤T≤5000), number of test cases. T pairs of lines with test case descriptions follow.

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

The second line of a test case description contains string s of length n 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 s 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 n 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).

Example
inputCopy
6
7
abacaba
7
???
11
aba?abacaba
11
abacaba?aba
15
asdf???f???qwer
11
abacabacaba
outputCopy
Yes
abacaba
Yes
abacaba
Yes
abadabacaba
Yes
abacabadaba
No
No
Note
In first example there is exactly one occurrence of a string “abacaba” in the string “abacaba” as a substring.

In second example seven question marks can be replaced with any seven lowercase English letters and with “abacaba” in particular.

In sixth example there are two occurrences of a string “abacaba” as a substring.

题意:将字符串中的?换成其他小写字母,使得字符串中只出现一次子字符串abacaba.
思路:
暴力枚举每个字符作为起始点判断是与abacaba否相同。

在枚举过程中,遇到替换?后形成子字符串abacaba时,将它的起始坐标储存在数组中。枚举完毕后,如果没有替换字符?时就有两个及以上子字符串abacaba,或所有的枚举中没有出现子字符串abacaba,可以判定不可能。

如果没有替换?时就有一个子字符串abacaba,那么直接将字符串中?换为字符z,输出即可。

如果,只有替换?时才形成子字符串abacaba,比如abac???caba字符可以生产两个子字符串abacaba,再次枚举起始点为中心长度-1为半径的所有点,如果这时,字符串中只有一个子字符串abacaba,如上输出,否则不可能。
#define ll long long
char s[5001];
char ss[]="abacaba";
int flag=0;//替换?形成的数量
int nums=0,n;//没替换就有的数量 长度
int store[5001];//起始点坐标
int test=1;//是否存储
void finds(int x)//查找以s[x]为起点是否能够生成
{
    int v=x;
    int q=0;
    for(int i=0; i<7; i++,x++)
    {
        if(ss[i]==s[x])
            continue;
        else if(s[x]=='?')
        {
            q=1;
            continue;
        }
        else
            return ;
    }
    if(q==0)
        nums++;
    else
        {if(test)store[++store[0]]=v;flag++;}
}
int main()
{
    int t;
    cin>>t;
    int dd=0;
    while(t--)
    {
        dd++;
        test=1;//储存
        cin>>n;
        cin>>s;
        nums=0;
        flag=0;
        int be=-1; store[0]=0;
        for(int i=0; i<n-6; i++)
        {
            finds(i);
            if(nums>1)
                break;
        }

        if(nums>1||(nums==0&&flag==0))//不可能条件
        {
            cout<<"NO"<<endl;
        }
        else
        {
            if(nums==0)
            {
                test=0;//不储存
                int pori[7],j=0;
                for(int i=1;i<=store[0];i++)//查找这些坐标是否有满足条件的
                {
                    nums=0;flag=0;
                    for(int j=store[i];j<store[i]+7;j++)
                    {
                        pori[j-store[i]]=s[j];//暂时储存
                        s[j]=ss[j-store[i]];
                    }
                    for(int j=max(0,store[i]-6);j<store[i]+7;j++)
                        finds(j);
                        if(nums==1){
                            j=1;break;
                        }
                        else{
                            for(int j=store[i];j<store[i]+7;j++)
                                s[j]=pori[j-store[i]];
                        }

                }
                if(j==0){//没有符合条件的
                    cout<<"NO"<<endl;
                    continue;
                }
            }
            cout<<"YES"<<endl;
            for(int i=0; i<n; i++)
            {
                cout<<(s[i]=='?'?'z':s[i]);
            }

            cout<<endl;

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值