Artifact Content
Not logged in

Artifact ea267041d7b81e1e97395e191c17886b2a6a02e2


#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>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

class ColorfulChocolates { public:
	int maximumSpread(string chocolates, int maxSwaps)
	{
		int N = chocolates.size();
		chocolates += '$';
		int best = 0;
		for(int s=0; s<N; ++s)
			for(int e=s+1; e<=N; ++e)
				for(char c='A'; c<='Z'; ++c)
					if( can(chocolates, c, s, e, maxSwaps) )
						best = max(best, count(chocolates.begin()+s, chocolates.begin()+e, c));
		return best;
	}

	bool can(const string& choco, char c, int s, int e, int k)
	{
		vector<int> z;
		for(;;) {
			int i = choco.find(c, s);
			if(i>=e || i==string::npos)
				i = e;

			z.push_back(i-s);
			if( i == e )
				break;
			s = i+1;
		}
		int needSwap = 0;
		for(int i=0; i<z.size(); ++i)
			needSwap += z[i] * min<int>(i, z.size()-1-i);
		return needSwap <= k;
	}
};

// 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(_, ColorfulChocolates().maximumSpread(chocolates, maxSwaps));}
int main(){

CASE(0)
	string chocolates = "ABCDCBC"; 
	int maxSwaps = 1; 
	int _ = 2; 
END
CASE(1)
	string chocolates = "ABCDCBC"; 
	int maxSwaps = 2; 
	int _ = 3; 
END
CASE(2)
	string chocolates = "ABBABABBA"; 
	int maxSwaps = 3; 
	int _ = 4; 
END
CASE(3)
	string chocolates = "ABBABABBA"; 
	int maxSwaps = 4; 
	int _ = 5; 
END
CASE(4)
	string chocolates = "QASOKZNHWNFODOQNHGQKGLIHTPJUVGKLHFZTGPDCEKSJYIWFOO"; 
	int maxSwaps = 77; 
	int _ = 5; 
END
CASE(5)
	string chocolates = "ABCDEBC"; 
	int maxSwaps = 1; 
	int _ = 1; 
END
/*
CASE(6)
	string chocolates = ; 
	int maxSwaps = ; 
	int _ = ; 
END
*/
}
// END CUT HERE