Artifact Content
Not logged in

Artifact 258b2dbb2f8262f71964559dc961a60830fbd136


#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 PrefixTree { public:
	static vector<int> wordToFreq( const string& word )
	{
		vector<int> freq(26);
		for(int i=0; i<word.size(); ++i)
			freq[word[i]-'a']++;
		return freq;
	}

	static vector< vector<int> > wordsToFreqs( const vector<string>& words )
	{
		vector< vector<int> > freqs;
		transform(words.begin(), words.end(), back_inserter(freqs), &wordToFreq);
		return freqs;
	}

	static int commonPrefix(const vector< vector<int> >& freqs, int s)
	{
		vector<int> common;
		for(int first=1,i=0; (1<<i)<=s; ++i)
			if( s & (1<<i) )
				if( first ) {
					common = freqs[i];
					first = 0;
				} else {
					for(int k=0; k<26; ++k)
						common[k] = min(common[k], freqs[i][k]);
				}
		return accumulate(common.begin(), common.end(), 0);
	}

	int getNumNodes(vector<string> words) 
	{
		const int N = words.size();
		const vector< vector<int> >& freqs = wordsToFreqs(words);

		vector<int> dp(1<<N);
		for(int s=1; s<(1<<N); ++s)
		{
			dp[s] = 1;
			for(int i=0; i<N; ++i)
				if( s & (1<<i) )
					dp[s] += words[i].size();

			int cp = commonPrefix(freqs, s);
			for(int ss=s-1; ss&=s; --ss)
				dp[s] = min(dp[s], dp[ss]+dp[s&~ss]-cp-1);
		}
		return dp[(1<<N)-1];
	}
};

// 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(_, PrefixTree().getNumNodes(words));}
int main(){

CASE(0)
	string words_[] = {"topcoder"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	int _ = 9; 
END
CASE(1)
	string words_[] = {"topcoder","topcoder"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	int _ = 9; 
END
CASE(2)
	string words_[] = {"aab","ca"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	int _ = 5; 
END
CASE(3)
	string words_[] = {"aab","ca","ba"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	int _ = 5; 
END
CASE(4)
	string words_[] = {"ab","cd","ef"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	int _ = 7; 
END
CASE(5)
	string words_[] = {"a","aa","aaa"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	int _ = 4; 
END
/*
CASE(6)
	string words_[] = ;
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	int _ = ; 
END
CASE(7)
	string words_[] = ;
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	int _ = ; 
END
*/
}
// END CUT HERE