A. Beautiful String
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 ss, 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+1si≠si+1 should be satisfied for all 1≤i≤|s|−11≤i≤|s|−1, where |s||s| is the length of the string ss.
Input
The first line contains positive integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Next tt lines contain the descriptions of test cases.
Each line contains a non-empty string ss consisting of only characters 'a', 'b', 'c' and '?'.
It is guaranteed that in each test case a string ss has at least one character '?'. The sum of lengths of strings ss in all test cases does not exceed 105105.
Output
For each test case given in the input print the answer in the following format:
- If it is impossible to create a beautiful string, print "-1" (without quotes);
- Otherwise, print the resulting beautiful string after replacing all '?' characters. If there are multiple answers, you can print any of them.
Example
input
Copy
3 a???cb a??bbc a?b?c
output
Copy
ababcb -1 acbac
题意:
给出一个由abc和?构成的字符串,问号可替换为a,b,c,如果替换后的字符串相邻字符都不相同,则称为完美字符串,并输出该字符串,若无法形成完美字符串,则输出-1
替换字符与问号左右的字符不相同即可
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
int main()
{
int t,n,i;
string a;
cin>>t;
while(t--)
{
int flag=0;
cin>>a;
for(i=0;i<a.length();i++)
{
if(a[i]=='?')
{
if(i!=0)
a[i]=(a[i-1]-'a'+1)%3+'a';
else
a[i]='a';
if(a[i+1]!='?')
if(a[i]==a[i+1])
a[i]=(a[i+1]-'a'+1)%3+'a';
}
}
for(i=0;i<a.length();i++)
{
if(a[i]==a[i+1])
{
flag=1;
break;
}
}
if(flag)
cout<<"-1"<<endl;
else
cout<<a<<endl;
}
return 0;
}