链接:https://codeforces.com/contest/1265/problem/A
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
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 44-th and 55-th characters will be always equal.
In the third test case, the only answer is "acbac".
代码:
#include <bits/stdc++.h>
using namespace std;
long long n,c;
char x[1000001];
long long mod=10007;
int main()
{
cin>>n;
while(n--)
{
cin>>x;
long long l=strlen(x);
int f=1;
x[l]='d';
for(int i=0;i<l;i++)
{
if(x[i]=='?')
{
if(i>0&&i<l-1)
{
int flag=0;
for(int j=0;j<=2;j++)
{
x[i]='a'+j;
if(x[i]!=x[i-1]&&x[i]!=x[i+1])
{
flag=1;
break;
}
}
if(flag==0)
{
f=0;
break;
}
}
else if(i==0)
{
int flag=0;
for(int j=0;j<=2;j++)
{
x[i]='a'+j;
if(x[i]!=x[i+1])
{
flag=1;
break;
}
}
if(flag==0)
{
f=0;
break;
}
}
else if(i==l-1)
{
int flag=0;
for(int j=0;j<=2;j++)
{
x[i]='a'+j;
if(x[i]!=x[i-1])
{
flag=1;
break;
}
}
if(flag==0)
{
f=0;
break;
}
}
}
else
{
if(x[i]==x[i+1])
{
f=0;
break;
}
}
}
if(f==0)
cout<<-1<<endl;
else
{
for(int i=0;i<l;i++)
cout<<x[i];
cout<<endl;
}
}
}
构造美丽字符串

该博客讨论了如何从包含'a', 'b', 'c'和'?'字符的字符串中构造美丽的字符串,其中美丽字符串定义为没有连续相同字符。题目要求在替换所有'?'字符后确保字符串满足相邻字符不相同的条件。博客提供了输入输出示例,并指出在某些情况下可能无法构造出美丽字符串。"
126374117,9038694,二叉树最大路径和深度优先解法,"['深度优先', '算法', '二叉树']
499

被折叠的 条评论
为什么被折叠?



