题解 | 小红的字符串构造
小红的字符串构造
https://www.nowcoder.com/practice/3e4b4dabc2e444e384c3ae62ac7dd84e
#include <bits/stdc++.h>
using namespace std;
/*
贪心,先用没有被用过的,如果都用过了,就随便找一个和当前字符不一样的,不能构成的条件就是只出现了一种字符
*/
string str;
map<char,bool> mp;
int main(){
cin>>str;
for(auto x : str) mp[x] = 1;
if(mp.size()==1) return cout<<-1,0;
for(int i = 0;i<str.size();i++){
char c = ' ';
for(auto x : mp){
if(x.second==0) continue;
if(x.first!=str[i]){
c = x.first;
mp[c] = 0;
break;
}
}
if(c==' '){
for(auto x : mp){
if(x.first!=str[i]){
c = x.first;
mp[c] = 0;
break;
}
}
}
cout<<c;
}
return 0;
}
#牛客春招刷题训练营#https://www.nowcoder.com/discuss/727521113110073344
