Codeforces-1265A ----- Beautiful String

题目来源:CodeForces - 1265A 点击进入原题

Description:

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:
3
a???cb
a??bbc
a?b?c
output:
ababcb
-1
acbac

题目大意:

对于输入的一个带有a,b,c,?的字符串来说,如果发现除去?号以外的其他的字符连续出现两次及以上,则认为这个字符串不是Beautiful String,输出-1,否则将?替换成a,b,c中的任意字符,判断他们是不是Beautiful String,是则输出替换后的字符串。

题目思路:

对于一个输入的样例字符串,只要字符串中出现了两个连续的相同的字符,则代表这个字符串不是Beautiful String,直接输出-1,否则它一定可以生成一个Beautiful String,这时我们只需要每次考虑当前这个问号所在的位置和它前、后一个字符不相同就可以。

AC代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
const int maxn=1e5+5;
using namespace std;
typedef long long ll;
char str[maxn];
char change(char s){
	switch(s)
	{
		case 'a':s='b';break;
		case 'b':s='c';break;
		case 'c':s='a';break;	
	}
	return s;
}

void solve(){
	int n;
	scanf("%d",&n);
	while(n--){
		scanf("%s",&str);
		int len=strlen(str);
		int flag=1;
		for(int i=0;i<len;i++){
			if(str[i]==str[i+1] && str[i]!='?'){
				flag=0;
				break;
			}
		}
		if(flag==0){
			printf("-1\n");
			continue;
		}
		else{
			for(int i=0;i<len;i++){
				if(str[i]=='?'){
					if(i==0)
						str[i]='a';
					else{
						str[i]=change(str[i-1]);
						}
                    //判断替换后的字符是否于后一个字符相同,相同则再次更改替换
					if(i+1<len && str[i]==str[i+1]){
						str[i]=change(str[i]);
					}
				}	
			}
			printf("%s\n",str);		
		}
	}
	return ;
}
int main(){
	solve();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值