2de557d8c0 2011-03-20 kinaba: #include <iostream> 2de557d8c0 2011-03-20 kinaba: #include <sstream> 2de557d8c0 2011-03-20 kinaba: #include <iomanip> 2de557d8c0 2011-03-20 kinaba: #include <vector> 2de557d8c0 2011-03-20 kinaba: #include <string> 2de557d8c0 2011-03-20 kinaba: #include <map> 2de557d8c0 2011-03-20 kinaba: #include <set> 2de557d8c0 2011-03-20 kinaba: #include <algorithm> 2de557d8c0 2011-03-20 kinaba: #include <numeric> 2de557d8c0 2011-03-20 kinaba: #include <iterator> 2de557d8c0 2011-03-20 kinaba: #include <functional> 2de557d8c0 2011-03-20 kinaba: #include <complex> 2de557d8c0 2011-03-20 kinaba: #include <queue> 2de557d8c0 2011-03-20 kinaba: #include <stack> 2de557d8c0 2011-03-20 kinaba: #include <cmath> 2de557d8c0 2011-03-20 kinaba: #include <cassert> 2de557d8c0 2011-03-20 kinaba: #include <cstring> 2de557d8c0 2011-03-20 kinaba: using namespace std; 2de557d8c0 2011-03-20 kinaba: typedef long long LL; 2de557d8c0 2011-03-20 kinaba: typedef complex<double> CMP; 2de557d8c0 2011-03-20 kinaba: 2de557d8c0 2011-03-20 kinaba: class MatchString { public: 2de557d8c0 2011-03-20 kinaba: int placeWords(string matchString, vector <string> matchWords) 2de557d8c0 2011-03-20 kinaba: { 2de557d8c0 2011-03-20 kinaba: const int N = matchString.size(); 2de557d8c0 2011-03-20 kinaba: vector< vector<int> > idx(N); 2de557d8c0 2011-03-20 kinaba: set<int> all_idx; 2de557d8c0 2011-03-20 kinaba: 2de557d8c0 2011-03-20 kinaba: for(int i=0; i<N; ++i) 2de557d8c0 2011-03-20 kinaba: { 2de557d8c0 2011-03-20 kinaba: for(int j=0; j<matchWords[i].size(); ++j) 2de557d8c0 2011-03-20 kinaba: if( matchWords[i][j] == matchString[i] ) 2de557d8c0 2011-03-20 kinaba: { 2de557d8c0 2011-03-20 kinaba: idx[i].push_back(j); 2de557d8c0 2011-03-20 kinaba: all_idx.insert(j); 2de557d8c0 2011-03-20 kinaba: } 2de557d8c0 2011-03-20 kinaba: if( idx[i].empty() ) 2de557d8c0 2011-03-20 kinaba: return -1; 2de557d8c0 2011-03-20 kinaba: } 2de557d8c0 2011-03-20 kinaba: 2de557d8c0 2011-03-20 kinaba: int best = INT_MAX; 2de557d8c0 2011-03-20 kinaba: for(set<int>::iterator it=all_idx.begin(); it!=all_idx.end(); ++it) 2de557d8c0 2011-03-20 kinaba: { 2de557d8c0 2011-03-20 kinaba: int T = *it; 2de557d8c0 2011-03-20 kinaba: int score = 0; 2de557d8c0 2011-03-20 kinaba: for(int i=0; i<N; ++i) 2de557d8c0 2011-03-20 kinaba: { 2de557d8c0 2011-03-20 kinaba: int bb = INT_MAX; 2de557d8c0 2011-03-20 kinaba: for(int j=0; j<idx[i].size(); ++j) 2de557d8c0 2011-03-20 kinaba: if( idx[i][j] <= T ) 2de557d8c0 2011-03-20 kinaba: bb = min(bb, T-idx[i][j]); 2de557d8c0 2011-03-20 kinaba: if( bb == INT_MAX ) 2de557d8c0 2011-03-20 kinaba: goto next; 2de557d8c0 2011-03-20 kinaba: score += bb; 2de557d8c0 2011-03-20 kinaba: } 2de557d8c0 2011-03-20 kinaba: best = min(best, score); 2de557d8c0 2011-03-20 kinaba: next:; 2de557d8c0 2011-03-20 kinaba: } 2de557d8c0 2011-03-20 kinaba: return best; 2de557d8c0 2011-03-20 kinaba: } 2de557d8c0 2011-03-20 kinaba: }; 2de557d8c0 2011-03-20 kinaba: 2de557d8c0 2011-03-20 kinaba: // BEGIN CUT HERE 2de557d8c0 2011-03-20 kinaba: #include <ctime> 2de557d8c0 2011-03-20 kinaba: double start_time; string timer() 2de557d8c0 2011-03-20 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 2de557d8c0 2011-03-20 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 2de557d8c0 2011-03-20 kinaba: { os << "{ "; 2de557d8c0 2011-03-20 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 2de557d8c0 2011-03-20 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 2de557d8c0 2011-03-20 kinaba: void verify_case(const int& Expected, const int& Received) { 2de557d8c0 2011-03-20 kinaba: bool ok = (Expected == Received); 2de557d8c0 2011-03-20 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 2de557d8c0 2011-03-20 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 2de557d8c0 2011-03-20 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 2de557d8c0 2011-03-20 kinaba: #define END verify_case(_, MatchString().placeWords(matchString, matchWords));} 2de557d8c0 2011-03-20 kinaba: int main(){ 2de557d8c0 2011-03-20 kinaba: 2de557d8c0 2011-03-20 kinaba: CASE(0) 2de557d8c0 2011-03-20 kinaba: string matchString = "TOP"; 2de557d8c0 2011-03-20 kinaba: string matchWords_[] = {"TIK", 2de557d8c0 2011-03-20 kinaba: "PPPO", 2de557d8c0 2011-03-20 kinaba: "OP"}; 2de557d8c0 2011-03-20 kinaba: vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 2de557d8c0 2011-03-20 kinaba: int _ = 5; 2de557d8c0 2011-03-20 kinaba: END 2de557d8c0 2011-03-20 kinaba: CASE(1) 2de557d8c0 2011-03-20 kinaba: string matchString = "EEA"; 2de557d8c0 2011-03-20 kinaba: string matchWords_[] = {"GEGA", 2de557d8c0 2011-03-20 kinaba: "TOPCODER", 2de557d8c0 2011-03-20 kinaba: "TEST"}; 2de557d8c0 2011-03-20 kinaba: vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 2de557d8c0 2011-03-20 kinaba: int _ = -1; 2de557d8c0 2011-03-20 kinaba: END 2de557d8c0 2011-03-20 kinaba: CASE(2) 2de557d8c0 2011-03-20 kinaba: string matchString = "AB"; 2de557d8c0 2011-03-20 kinaba: string matchWords_[] = {"ABA", 2de557d8c0 2011-03-20 kinaba: "ABAB"}; 2de557d8c0 2011-03-20 kinaba: vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 2de557d8c0 2011-03-20 kinaba: int _ = 1; 2de557d8c0 2011-03-20 kinaba: END 2de557d8c0 2011-03-20 kinaba: CASE(3) 2de557d8c0 2011-03-20 kinaba: string matchString = "FIND"; 2de557d8c0 2011-03-20 kinaba: string matchWords_[] = {"VERYFAST", 2de557d8c0 2011-03-20 kinaba: "OPINION", 2de557d8c0 2011-03-20 kinaba: "SPENDING", 2de557d8c0 2011-03-20 kinaba: "ODD"}; 2de557d8c0 2011-03-20 kinaba: vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 2de557d8c0 2011-03-20 kinaba: int _ = 3; 2de557d8c0 2011-03-20 kinaba: END 2de557d8c0 2011-03-20 kinaba: CASE(4) 2de557d8c0 2011-03-20 kinaba: string matchString = "TOP"; 2de557d8c0 2011-03-20 kinaba: string matchWords_[] = {"OUTTHERE", 2de557d8c0 2011-03-20 kinaba: "FROM", 2de557d8c0 2011-03-20 kinaba: "NOPQRSTU"}; 2de557d8c0 2011-03-20 kinaba: vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 2de557d8c0 2011-03-20 kinaba: int _ = 0; 2de557d8c0 2011-03-20 kinaba: END 2de557d8c0 2011-03-20 kinaba: /* 2de557d8c0 2011-03-20 kinaba: CASE(5) 2de557d8c0 2011-03-20 kinaba: string matchString = ; 2de557d8c0 2011-03-20 kinaba: string matchWords_[] = ; 2de557d8c0 2011-03-20 kinaba: vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 2de557d8c0 2011-03-20 kinaba: int _ = ; 2de557d8c0 2011-03-20 kinaba: END 2de557d8c0 2011-03-20 kinaba: CASE(6) 2de557d8c0 2011-03-20 kinaba: string matchString = ; 2de557d8c0 2011-03-20 kinaba: string matchWords_[] = ; 2de557d8c0 2011-03-20 kinaba: vector <string> matchWords(matchWords_, matchWords_+sizeof(matchWords_)/sizeof(*matchWords_)); 2de557d8c0 2011-03-20 kinaba: int _ = ; 2de557d8c0 2011-03-20 kinaba: END 2de557d8c0 2011-03-20 kinaba: */ 2de557d8c0 2011-03-20 kinaba: } 2de557d8c0 2011-03-20 kinaba: // END CUT HERE