Artifact Content
Not logged in

Artifact 15460ab419ea2b8ce0d4c8f0134e380190934508


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

struct UnionFind
{
	vector<int> uf, sz;
	int nc;

	UnionFind(int N): uf(N), sz(N,1), nc(N)
		{ for(int i=0; i<N; ++i) uf[i] = i; }
	int size()
		{ return nc; }
	int size(int a)
		{ return sz[Find(a)]; }
	int Find(int a)
		{ return uf[a]==a ? a : uf[a]=Find(uf[a]); }
	bool Union(int a, int b)
		{
			a = Find(a);
			b = Find(b);
			if( a != b )
			{
				if( sz[a] >= sz[b] ) swap(a, b);
				uf[a] = b;
				sz[b] += sz[a];
				--nc;
			}
			return (a!=b);
		}
};

class GooseTattarrattatDiv1 { public:
	int getmin(string S)
	{
		UnionFind uf(128);

		map<char,int> time_for;
		for(int i=0; i<S.size(); ++i)
		{
			time_for[S[i]]++;
			uf.Union(S[i], S[S.size()-1-i]);
		}

		int total = 0;

		set<char> done;
		for(auto c : S)
		{
			if(done.count(c))
				continue;

			int need = 0;
			int m = 0;
			for(auto d : S)
				if(uf.Find(c) == uf.Find(d)) {
					if(done.count(d))
						continue;
					done.insert(d);
					need += time_for[d];
					m = max(m, time_for[d]);
				}
			total += (need - m);
		}

		return total;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, GooseTattarrattatDiv1().getmin(S));}
int main(){

CASE(0)
	string S = "geese"; 
	int _ = 2; 
END
CASE(1)
	string S = "tattarrattat"; 
	int _ = 0; 
END
CASE(2)
	string S = "xyyzzzxxx"; 
	int _ = 2; 
END
CASE(3)
	string S = "xrepayuyubctwtykrauccnquqfuqvccuaakylwlcjuyhyammag"; 
	int _ = 11; 
END
CASE(4)
	string S = "abaabb"; 
	int _ = 3; 
END
/*
CASE(5)
	string S = ; 
	int _ = ; 
END
CASE(6)
	string S = ; 
	int _ = ; 
END
*/
}
// END CUT HERE