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.
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.
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.
3
a???cb
a??bbc
a?b?c
ababcb
-1
acbac
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 44-th and 55-th characters will be always equal.
In the third test case, the only answer is “acbac”.
如果遇到两个连续非‘?’相等,则输出-1。如果遇到‘?’则做变换,变为不与前后字符相同的字符。
1.遇到‘?’时,直接选择和前一字符不同的字符填入,同时也要注意后一字符是否为问号,避免与后一字符相同。
2.注意数字和字母之间的ASCII码转换,a对应97。
#include<iostream>
#include<algorithm>
#include<cstdio>using namespace std;char s[100001];
int main()
{int t;scanf("%d",&t);while(t--){scanf("%s",s);int flag=0;int l=strlen(s);//字符串s的长度for(int i=0;i<l;i++){if(s[i]==s[i+1]&&s[i]!='?')//与相邻字符相同且非‘?’{flag=1;//进行标记break;}if(s[i]=='?'){int count=1;while(s[i]=='?'||s[i]==s[i-1]||s[i]==s[i+1])//为‘?’且与前后字符相同s[i]=(count++)%3+97;//注意ASCII值的转换}}if(flag)printf("-1n");elseprintf("%sn",s);}return 0;
}
本文发布于:2024-01-28 13:45:13,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064207177832.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |