Artifact Content
Not logged in

Artifact 2023aee3feb683a108b3ae88e50fb756d925db92


#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<double> CMP;

class UndoHistory { public:
	int minPresses(vector <string> lines)
	{
		int score = lines[0].size() + 1;
		for(int i=1; i<lines.size(); ++i)
		{
			int best = 0x7fffffff;
			for(int k=0; k<i; ++k) {
				int pm = prefix_match(lines[i], lines[k]);
				if(k<i-1) {
					best = min<int>(best, 2 + lines[i].size()-pm);
				} else {
					if(pm == lines[k].size())
						best = min<int>(best, lines[i].size() - pm);
					else
						best = min<int>(best, 2 + lines[i].size() - pm);
				}
			}
			score += best+1;
		}
		return score;
	}

	int prefix_match(const string& a, const string& b)
	{
		int i = 0;
		while(i<a.size() && i<b.size() && a[i]==b[i])
			++i;
		return i;
	}
};

// 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(_, UndoHistory().minPresses(lines));}
int main(){

CASE(0)
	string lines_[] = {"tomorrow", "topcoder"};
	  vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 
	int _ = 18; 
END
CASE(1)
	string lines_[] = {"a","b"};
	  vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 
	int _ = 6; 
END
CASE(2)
	string lines_[] = {"a", "ab", "abac", "abacus" };
	  vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 
	int _ = 10; 
END
CASE(3)
	string lines_[] = {"pyramid", "sphinx", "sphere", "python", "serpent"};
	  vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 
	int _ = 39; 
END
CASE(4)
	string lines_[] = {"ba","a","a","b","ba"}
;
	  vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 
	int _ = 13; 
END
/*
CASE(5)
	string lines_[] = ;
	  vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 
	int _ = ; 
END
CASE(6)
	string lines_[] = ;
	  vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 
	int _ = ; 
END
	*/
}
// END CUT HERE