题解 | #小红的字符串构造#
小红的字符串构造
https://www.nowcoder.com/practice/3e4b4dabc2e444e384c3ae62ac7dd84e
#include <bits/stdc++.h> using namespace std; int __t = 1, n; void solve() { string s; cin >> s; map <char,char> m; int len =s.size(); for(int i=0;i<len;i++){ m[s[i]]=s[i];//建立原字符串字符集 }if(m.size()==1){ cout<<-1; return; } char cend=m.rbegin()->second; for(auto& [k,v]:m){ swap(v,cend);//修改原字符串字符集,即原为[a,a],改后为[a,c],[b,a],[c,a]以便下面的寻值 }for(auto i:s){ cout<<m[i]; }cout<<'\n'; return; } int main() { // cin >> __t; while (__t--) solve(); return 0; }
这题比较简单,只要求不是原字母即可
之前还见过在此基础上要求只能使用原字符串的字母,不像现在不限制使用的字母个数,即使超过原字符串那位字母的个数
#牛客创作赏金赛#