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.
Input
The first line contains positive integer t (1≤t≤1000) — the number of test cases. Next t lines contain the descriptions of test
cases.
Each line contains a non-empty string s consisting of only characters
‘a’, ‘b’, ‘c’ and ‘?’. It is guaranteed that in each test case a
string s has at least one character ‘?’. The sum of lengths of strings
s in all test cases does not exceed 105.
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
3 a???cb
a??bbc
a?b?cOutput
ababcb
-1
acbac
Note
In the first test case, all possible correct answers are “ababcb”, “abcacb”, “abcbcb”, “acabcb” and “acbacb”. The two answers
“abcbab” and “abaabc” are incorrect, because you can replace only ‘?’
characters and the resulting string must be beautiful. In the second
test case, it is impossible to create a beautiful string, because the
4-th and 5-th characters will be always equal. In the third test case,
the only answer is “acbac”.
题意
给一个仅由a,b,c,?构成的字符串,要求把所有问号变成a/b/c,使任意相邻的两个字符不相同。
思路
碰到?改为与前后字符不相同的字符
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,x;
cin >> n;
char str[1000];
while(n>0){
int flag = 1;
cin >> str;
x = strlen (str);
if(str[0] == '?'){
if(str[1] != 'a')
str[0] = 'a';
else if(str[1] != 'b')
str[0] = 'b';
else if(str[1] != 'a')
str[0] = 'c';
}
else{
for(int i=0; i<x; i++){
if(str[i] == '?'){
if(str[i+1]!='a'&&str[i-1]!='a')
str[i]='a';
else if(str[i+1]!='b'&&str[i-1]!='b')
str[i]='b';
else if(str[i+1]!='c'&&str[i-1]!='c')
str[i]='c';
}
else{
if(str[i] == str[i+1]){
flag = 0;
break;}
}
}
}
if(flag == 0)
cout<<"-1"<<endl;
else
cout<<str<<endl;
}
}