Artifact Content
Not logged in

Artifact f85d7cb8c1261a548e39a0801328dd5a37eaee6e


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

class MatchString { public:
	int placeWords(string matchString, vector <string> matchWords) 
	{
		const int N = matchString.size();
		vector< vector<int> > idx(N);
		set<int> all_idx;

		for(int i=0; i<N; ++i)
		{
			for(int j=0; j<matchWords[i].size(); ++j)
				if( matchWords[i][j] == matchString[i] )
				{
					idx[i].push_back(j);
					all_idx.insert(j);
				}
			if( idx[i].empty() )
				return -1;
		}

		int best = INT_MAX;
		for(set<int>::iterator it=all_idx.begin(); it!=all_idx.end(); ++it)
		{
			int T = *it;
			int score = 0;
			for(int i=0; i<N; ++i)
			{
				int bb = INT_MAX;
				for(int j=0; j<idx[i].size(); ++j)
					if( idx[i][j] <= T )
						bb = min(bb, T-idx[i][j]);
				if( bb == INT_MAX )
					goto next;
				score += bb;
			}
			best = min(best, score);
		next:;
		}
		return best;
	}
};

// 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(_, MatchString().placeWords(matchString, matchWords));}
int main(){

CASE(0)
	string matchString = "TOP"; 
	string matchWords_[] = {"TIK", 
 "PPPO", 
 "OP"};
	  vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 
	int _ = 5; 
END
CASE(1)
	string matchString = "EEA"; 
	string matchWords_[] = {"GEGA", 
 "TOPCODER", 
 "TEST"};
	  vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 
	int _ = -1; 
END
CASE(2)
	string matchString = "AB"; 
	string matchWords_[] = {"ABA", 
 "ABAB"};
	  vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 
	int _ = 1; 
END
CASE(3)
	string matchString = "FIND"; 
	string matchWords_[] = {"VERYFAST", 
 "OPINION", 
 "SPENDING", 
 "ODD"};
	  vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 
	int _ = 3; 
END
CASE(4)
	string matchString = "TOP"; 
	string matchWords_[] = {"OUTTHERE", 
 "FROM", 
 "NOPQRSTU"};
	  vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 
	int _ = 0; 
END
/*
CASE(5)
	string matchString = ; 
	string matchWords_[] = ;
	  vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 
	int _ = ; 
END
CASE(6)
	string matchString = ; 
	string matchWords_[] = ;
	  vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 
	int _ = ; 
END
*/
}
// END CUT HERE