B. Hate “A”
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Bob has a string s consisting of lowercase English letters. He defines s′ to be the string after removing all “a” characters from s (keeping all other characters in the same order). He then generates a new string t by concatenating s and s′. In other words, t=s+s′ (look at notes for an example).
You are given a string t. Your task is to find some s that Bob could have used to generate t. It can be shown that if an answer exists, it will be unique.
Input
The first line of input contains a string t (1≤|t|≤105) consisting of lowercase English letters.
Output
Print a string s that could have generated t. It can be shown if an answer exists, it is unique. If no string exists, print “?” (without double quotes, there is no space between the characters).
Examples
input
aaaaa
output
aaaaa
input
aacaababc
output
?
input
ababacacbbcc
output
ababacac
input
baba
output
?
Note
In the first example, we have s= “aaaaa”, and s′= “”.
In the second example, no such s can work that will generate the given t.
In the third example, we have s= “ababacac”, and s′= “bbcc”, and t=s+s′= “ababacacbbcc”.
思路:
借鉴一个大佬的思路,string类型字符串先将去a后的存储在另一的char类型的数组中,接下来看char类型的字符串长度是否为偶数,如果是奇数就输出哭脸,偶数就看string类型的字符串的第string类型字符串长度减char类型的字符串的一半,与char类型的前一半进行比较,如果都一样输出,前string类型字符串长度减char类型的字符串长度的一半就行了,如果不然,输出哭脸。、
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
using namespace std;
ll gar(ll a,ll b){//最大公约数
return b==0?a:gar(b,a%b);
}
int main(){
string s;
while(cin>>s){
char s1[1000000];int i=0,j=0,len=s.length(),k=0;
for(;i<len;i++){
if(s[i]!='a'){
s1[j++]=s[i];
}
}//string类型字符串先将去a后的存储在另一的char类型的数组中
if(j%2!=0){
cout<<":("<<endl;
continue;
}//接下来看char类型的字符串长度是否为偶数,如果是奇数就输出哭脸
for(i=len-j/2;i<len;i++){
if(s[i]==s1[k]){
k++;
}
}/偶数就看string类型的字符串的第string类型字符串长度减char类型的字符串的一半,与char类型的前一半进行比较,
if(j/2==k)
{
for(i=0;i<(len-j/2);i++){
cout<<s[i];
}
cout<<endl;
} //如果都一样输出,前string类型字符串长度减char类型的字符串长度的一半就行了
else{
cout<<":("<<endl;
}//如果不然,输出哭脸。
}
return 0;
}