CF 604 div2 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 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:

  1. If it is impossible to create a beautiful string, print “-1” (without quotes);
  2. Otherwise, print the resulting beautiful string after replacing all ‘?’ characters.If there are multiple answers, you can print any of them.

Example
Input
a???cb
a??bbc
a?b?c
Output
ababcb
-1
acbac

考点

STL

题意

给你一个含a,b,c,?的字符串,将?改为a,b,c,在保证相邻字符互异的基础上。

思路

题目很简单,无脑STL即可,考虑全面,注意’?'在第一个和最后一个字符位置的情况和只有一个字符的情况即可。

AC代码

#include<bits/stdc++.h>
#include <time.h>
#include <iomanip>
#define _CRT_SECURE_NO_WARNINGSing namespace std;
int main () {
 ios::sync_with_stdio(false);
 cin.tie(0);cout.tie(0);
 int t;cin>>t;
 out:
 while(t--){
  string s;
  cin>>s;
  string::iterator it,pos;
  for(it=s.begin();it!=s.end();it++){
   pos=it;
   if(*pos!='?'){
    if(*pos==*(pos+1)){
     cout<<-1<<endl;
     goto out;
    }
   }
  }
  int len=s.size();
  if(len==1){
   for(it=s.begin();it!=s.end();it++){
    if(*it!='?'){
     cout<<*it<<endl;
    }
    else {
     cout<<'a'<<endl;
    }
   }
  }
  else{
   for(it=s.begin();it!=s.end();it++){
   pos=it;
   if(*pos=='?'&&pos==s.begin()&&(*(pos+1)=='a'||*(pos+1)=='b'||*(pos+1)=='?')){
    s.replace(s.find('?'),1,"c");
   }
   else if(*pos=='?'&&pos==s.begin()&&(*(pos+1)=='a'||*(pos+1)=='c'||*(pos+1)=='?')){
    s.replace(s.find('?'),1,"b");
   }
   else if(*pos=='?'&&pos==s.begin()&&(*(pos+1)=='b'||*(pos+1)=='c'||*(pos+1)=='?')){
    s.replace(s.find('?'),1,"a");
   }
   else if(*pos=='?'&&pos==s.end()-1&&(*(pos-1)=='a'||*(pos-1)=='b'||*(pos-1)=='?')){
    s.replace(s.find('?'),1,"c");
   }
   else if(*pos=='?'&&pos==s.end()-1&&(*(pos-1)=='a'||*(pos-1)=='c'||*(pos-1)=='?')){
    s.replace(s.find('?'),1,"b");
   }
   else if(*pos=='?'&&pos==s.end()-1&&(*(pos-1)=='b'||*(pos-1)=='c'||*(pos-1)=='?')){
    s.replace(s.find('?'),1,"a");
   }
   else if(*pos=='?'&&(*(pos-1)=='a'||*(pos-1)=='b')&&(*(pos+1)=='a'||*(pos+1)=='b'||*(pos+1)=='?')){
    s.replace(s.find('?'),1,"c");
   }
   else if(*pos=='?'&&(*(pos-1)=='b'||*(pos-1)=='c')&&(*(pos+1)=='b'||*(pos+1)=='c'||*(pos+1)=='?')){
    s.replace(s.find('?'),1,"a");
   }
   else if(*pos=='?'&&(*(pos-1)=='a'||*(pos-1)=='c')&&(*(pos+1)=='a'||*(pos+1)=='c'||*(pos+1)=='?')){
    s.replace(s.find('?'),1,"b");
   }
  }
  cout<<s<<endl;
  } 
 }
}
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页