SRM627 div1 Easy HappyLetterDiv1
概要
文字列S(|S|<=50)が与えられる。この文字列から異なる文字の2つ組を取り除く処理を繰り返していき最後に残る可能性のある文字の集合を求めろ。
解法
文字cが最後に残る可能性があるかどうかについて考える。文字c以外を全て消せるかどうか判定できればよい。最大の文字の個数<=それ以外の文字の個数で文字数が偶数であれば全てぴったり消すことができる。最大の文字の個数<=それ以外の文字の個数で文字数が奇数であれば1文字残る。最大の文字の個数>それ以外の文字の個数であれば(最大の文字の個数)-(それ以外の文字の個数)が残る。
残る文字数より文字cの数が多ければ文字cが最後に残る可能性がある。
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VL; typedef vector<VL> VVL; typedef pair<int, int> PII; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() #define IN(a, b, x) (a<=x&&x<b) #define MP make_pair #define PB push_back const int INF = (1LL<<30); const ll LLINF = (1LL<<60); const double PI = 3.14159265359; const double EPS = 1e-12; const int MOD = 1000000007; //#define int ll template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; class HappyLetterDiv1 { public: string getHappyLetters(string s) { int n = s.size(); int cnt[26] = {}; REP(i, n) cnt[s[i]-'a']++; string ret = ""; REP(i, 26) { if(cnt[i] == 0) continue; int ma = 0, su = 0; REP(j, 26) if(i != j) { su += cnt[j]; chmax(ma, cnt[j]); } if(cnt[i] == 1 && su%2) continue; if(ma > su-ma) { if(ma - (su-ma) >= cnt[i]) continue; } ret += (char)('a'+i); } return ret; } };