Skip to content

Instantly share code, notes, and snippets.

@iseki-masaya
Created May 30, 2015 03:11
Show Gist options
  • Save iseki-masaya/3389422b281be76566f0 to your computer and use it in GitHub Desktop.
Save iseki-masaya/3389422b281be76566f0 to your computer and use it in GitHub Desktop.
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <list>
#include <algorithm>
#include <sstream>
#include <set>
#include <cmath>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cstdlib>
#include <cstring>
#include <numeric>
#include <bitset>
#include <deque>
#include <memory>
const long long LINF = (5e17);
const int INF = 1000000000;
#define EPS 1e-7
const int MOD = 1000000007;
using namespace std;
class GooseTattarrattatDiv1 {
public:
int getmin(string S) {
int N = (int)S.size();
map<char, vector<int> > d;
for (int i=0; i<N; ++i)
d[S[i]].push_back(i);
vector<bool> used(N, false);
int ans = 0;
for (int i=0; i<N; ++i) {
if (used[i])
continue;
map<char, int> m;
queue<int> que;
for (int v: d[S[i]])
que.push(v);
while (!que.empty()) {
int v = que.front();
que.pop();
if (used[v])
continue;
used[v] = true;
m[S[v]]++;
int inv = N - v - 1;
if (!used[inv]) {
que.push(inv);
for (int k: d[S[inv]])
que.push(k);
}
}
int sum = 0;
int mx = 0;
for (auto p: m) {
sum += p.second;
mx = max(mx, p.second);
}
ans += sum - mx;
}
return ans;
}
// BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { string Arg0 = "geese"; int Arg1 = 2; verify_case(0, Arg1, getmin(Arg0)); }
void test_case_1() { string Arg0 = "tattarrattat"; int Arg1 = 0; verify_case(1, Arg1, getmin(Arg0)); }
void test_case_2() { string Arg0 = "xyyzzzxxx"; int Arg1 = 2; verify_case(2, Arg1, getmin(Arg0)); }
void test_case_3() { string Arg0 = "xrepayuyubctwtykrauccnquqfuqvccuaakylwlcjuyhyammag"; int Arg1 = 11; verify_case(3, Arg1, getmin(Arg0)); }
void test_case_4() { string Arg0 = "abaabb"; int Arg1 = 3; verify_case(4, Arg1, getmin(Arg0)); }
// END CUT HERE
};
// BEGIN CUT HERE
int main() {
GooseTattarrattatDiv1 ___test;
___test.run_test(-1);
}
// END CUT HERE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment