https://yukicoder.me/problems/no/587
解説
https://yukicoder.me/submissions/213849
まず、mapを使って、各文字について個数を数える。
文字の個数が1個のもの(one)と2個のもの(two)をカウントする。
このとき、3個以上のものがあれば"Impossible"
あとは、one==1かつtwo==6であれば、正しいチートイツが作れる。
そうでないなら"Impossible"
答える文字はoneを数える時に、別の変数に退避させておくとよい。
string S; //--------------------------------------------------------------------------------------------------- #define NO "Impossible" string solve() { map<char, int> cnt; fore(c, S) cnt[c]++; int one = 0, two = 0; char one_c; fore(p, cnt) { if (p.second == 1) one++, one_c = p.first; else if (p.second == 2) two++; else return NO; } if (one == 1 and two == 6) { string ans = "a"; ans[0] = one_c; return ans; } return NO; } //--------------------------------------------------------------------------------------------------- void _main() { cin >> S; cout << solve() << endl; }