be86f38991 2013-09-07 kinaba: #include <iostream> be86f38991 2013-09-07 kinaba: #include <sstream> be86f38991 2013-09-07 kinaba: #include <iomanip> be86f38991 2013-09-07 kinaba: #include <vector> be86f38991 2013-09-07 kinaba: #include <string> be86f38991 2013-09-07 kinaba: #include <map> be86f38991 2013-09-07 kinaba: #include <set> be86f38991 2013-09-07 kinaba: #include <algorithm> be86f38991 2013-09-07 kinaba: #include <numeric> be86f38991 2013-09-07 kinaba: #include <iterator> be86f38991 2013-09-07 kinaba: #include <functional> be86f38991 2013-09-07 kinaba: #include <complex> be86f38991 2013-09-07 kinaba: #include <queue> be86f38991 2013-09-07 kinaba: #include <stack> be86f38991 2013-09-07 kinaba: #include <cmath> be86f38991 2013-09-07 kinaba: #include <cassert> be86f38991 2013-09-07 kinaba: #include <tuple> be86f38991 2013-09-07 kinaba: using namespace std; be86f38991 2013-09-07 kinaba: typedef long long LL; be86f38991 2013-09-07 kinaba: typedef complex<double> CMP; be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: struct UnionFind be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: vector<int> uf, sz; be86f38991 2013-09-07 kinaba: int nc; be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: UnionFind(int N): uf(N), sz(N,1), nc(N) be86f38991 2013-09-07 kinaba: { for(int i=0; i<N; ++i) uf[i] = i; } be86f38991 2013-09-07 kinaba: int size() be86f38991 2013-09-07 kinaba: { return nc; } be86f38991 2013-09-07 kinaba: int size(int a) be86f38991 2013-09-07 kinaba: { return sz[Find(a)]; } be86f38991 2013-09-07 kinaba: int Find(int a) be86f38991 2013-09-07 kinaba: { return uf[a]==a ? a : uf[a]=Find(uf[a]); } be86f38991 2013-09-07 kinaba: bool Union(int a, int b) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: a = Find(a); be86f38991 2013-09-07 kinaba: b = Find(b); be86f38991 2013-09-07 kinaba: if( a != b ) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: if( sz[a] >= sz[b] ) swap(a, b); be86f38991 2013-09-07 kinaba: uf[a] = b; be86f38991 2013-09-07 kinaba: sz[b] += sz[a]; be86f38991 2013-09-07 kinaba: --nc; be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: return (a!=b); be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: }; be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: class GooseTattarrattatDiv1 { public: be86f38991 2013-09-07 kinaba: int getmin(string S) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: UnionFind uf(128); be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: map<char,int> time_for; be86f38991 2013-09-07 kinaba: for(int i=0; i<S.size(); ++i) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: time_for[S[i]]++; be86f38991 2013-09-07 kinaba: uf.Union(S[i], S[S.size()-1-i]); be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: int total = 0; be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: set<char> done; be86f38991 2013-09-07 kinaba: for(auto c : S) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: if(done.count(c)) be86f38991 2013-09-07 kinaba: continue; be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: int need = 0; be86f38991 2013-09-07 kinaba: int m = 0; be86f38991 2013-09-07 kinaba: for(auto d : S) be86f38991 2013-09-07 kinaba: if(uf.Find(c) == uf.Find(d)) { be86f38991 2013-09-07 kinaba: if(done.count(d)) be86f38991 2013-09-07 kinaba: continue; be86f38991 2013-09-07 kinaba: done.insert(d); be86f38991 2013-09-07 kinaba: need += time_for[d]; be86f38991 2013-09-07 kinaba: m = max(m, time_for[d]); be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: total += (need - m); be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: return total; be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: }; be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: // BEGIN CUT HERE be86f38991 2013-09-07 kinaba: #include <ctime> be86f38991 2013-09-07 kinaba: double start_time; string timer() be86f38991 2013-09-07 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } be86f38991 2013-09-07 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) be86f38991 2013-09-07 kinaba: { os << "{ "; be86f38991 2013-09-07 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) be86f38991 2013-09-07 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } be86f38991 2013-09-07 kinaba: void verify_case(const int& Expected, const int& Received) { be86f38991 2013-09-07 kinaba: bool ok = (Expected == Received); be86f38991 2013-09-07 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; be86f38991 2013-09-07 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } be86f38991 2013-09-07 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); be86f38991 2013-09-07 kinaba: #define END verify_case(_, GooseTattarrattatDiv1().getmin(S));} be86f38991 2013-09-07 kinaba: int main(){ be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: CASE(0) be86f38991 2013-09-07 kinaba: string S = "geese"; be86f38991 2013-09-07 kinaba: int _ = 2; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(1) be86f38991 2013-09-07 kinaba: string S = "tattarrattat"; be86f38991 2013-09-07 kinaba: int _ = 0; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(2) be86f38991 2013-09-07 kinaba: string S = "xyyzzzxxx"; be86f38991 2013-09-07 kinaba: int _ = 2; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(3) be86f38991 2013-09-07 kinaba: string S = "xrepayuyubctwtykrauccnquqfuqvccuaakylwlcjuyhyammag"; be86f38991 2013-09-07 kinaba: int _ = 11; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(4) be86f38991 2013-09-07 kinaba: string S = "abaabb"; be86f38991 2013-09-07 kinaba: int _ = 3; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: /* be86f38991 2013-09-07 kinaba: CASE(5) be86f38991 2013-09-07 kinaba: string S = ; be86f38991 2013-09-07 kinaba: int _ = ; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(6) be86f38991 2013-09-07 kinaba: string S = ; be86f38991 2013-09-07 kinaba: int _ = ; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: */ be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: // END CUT HERE