Codeforces #604 Div2 A

Codeforces #604 Div2 a

题目链接

题意:给带?号的只含abc的字符串,判断是否能够给出不含连续aa,bb,cc的串

思路:
如果没有连续的字母,那么一定能够找到对应的串。
只需要遍历一遍字符串即可找到答案

AC代码:

#include<bits/stdc++.h>
using namespace std;
bool qwq;
char s[100500];
void check(int x){
    s[x]='a';
    //如果一开始为?,则判断下一个是不是a,若是,则变为b或者c,此次变为b;
    if(s[0]=='?'){
        if(s[1]=='a'){
            s[0]='b';
        }
    }
    //否则判断前后是否为a,b,c,确定此次填入的字符值;
    else{
        if(s[x-1]=='a'||s[x+1]=='a'){
            s[x]='b';
            if(s[x-1]=='b'||s[x+1]=='b'){
                s[x]='c';
                if(s[x-1]=='c'||s[x+1]=='c'){
                    qwq=false;//若a,b,c均不能填,则false;
                }
            }
        }
    }
}
int  main(){
    int n;
    cin>>n;
    getchar();
    while(n--){
        qwq=true;
        gets(s);
        int len=strlen(s);
        for(int i=0;i<=len;i++){
            if(qwq==false) break;//进行上次计算false了,结束循环
            if(s[i]!=0&&s[i]==s[i-1])
            qwq=false;//若一开始后面的与前面的一样则false;
            if(s[i]=='?'){
                check(i);//每次更改问号的值,这样才能判断上一个if;
            }
        }
        //输出;
        if(qwq==false){
            cout<<-1<<endl;
        }
        else {
            for(int i=0;i<len;i++){
                cout<<s[i];
            }
            cout<<endl;
        }
    }
}

学习记录

发布了9 篇原创文章 · 获赞 0 · 访问量 285

猜你喜欢

转载自blog.csdn.net/sakuyu_10/article/details/103425833
今日推荐