Official
B - The Odd One Out Editorial
by
B - The Odd One Out Editorial
by
physics0523
この問題にはいくつかの方針があります。
方針1. 各文字の数を数える
各文字が何回現れるかを数えた上で、 \(1\) 度しか登場しない文字がただひとつに定まるので、それを出力すればよいです。
実装例 (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
map<char,int> mp;
for(auto &nx : s){ mp[nx]++; }
for(auto &nx : mp){
if(nx.second==1){
cout << nx.first << "\n";
}
}
return 0;
}
方針2. 問題の性質を利用する
与えられた文字列について、文字を辞書順にソートすると、正解となる文字は先頭か末尾に来るはずです。
更に言うと、ソート後の文字列 \(S'\) について、
- \(S'\) の \(1\) 文字目と \(2\) 文字目が異なるなら、正答は \(S'\) の \(1\) 文字目
- そうでないなら、正答は \(S'\) の末尾の文字
が成り立ちます。
実装例 (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
sort(s.begin(),s.end());
if(s[0]!=s[1]){cout << s[0] << "\n";}
else{cout << s.back() << "\n";}
return 0;
}
posted:
last update:
