Artifact Content
Not logged in

Artifact 0980b7195d8f96ca1040c3b34564725b5cb76e31


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

struct byLength {
	bool operator()(const string& lhs, const string& rhs) const {
		if(lhs.size() != rhs.size()) return lhs.size() < rhs.size();
		return lhs < rhs;
	}
};
class PrefixFreeSubsets { public:
	long long cantPrefFreeSubsets(vector <string> words) 
	{
		int N = words.size();
		sort(words.begin(), words.end(), byLength());

		vector< vector<int> > dc;
		for(int i=0; i<N; ++i)
			dc.push_back( direct_children(words[i], words, i+1) );
		dc.push_back( direct_children("", words, 0) );
		return rec(N, dc) - 1;
	}

	LL rec(int t, vector< vector<int> >& dc )
	{
		LL p = 1;
		for(int i=0; i<dc[t].size(); ++i)
			p *= rec(dc[t][i], dc);
		return p+1;
	}

	bool is_prefix(const string& pre, const string& all) {
		return equal(pre.begin(), pre.end(), all.begin());
	}

	vector<int> direct_children(const string& s, vector<string>& w, int i) {
		vector<int> ans;
		for(; i<w.size(); ++i)
			if( is_prefix(s,w[i]) ){
				bool direct = true;
				for(int j=0; j<ans.size(); ++j)
					direct = direct && !is_prefix(w[ans[j]], w[i]);
				if( direct )
					ans.push_back(i);
			}
		return ans;
	}
};

// 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 long long& Expected, const long long& 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(_, PrefixFreeSubsets().cantPrefFreeSubsets(words));}
int main(){

CASE(0)
	string words_[] = {"hello","hell","hi"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	long long _ = 6LL; 
END
CASE(1)
	string words_[] = {"a","b","c","d"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	long long _ = 16LL; 
END
CASE(2)
	string words_[] = {"a","ab","abc","abcd","abcde","abcdef"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	long long _ = 7LL; 
END
CASE(3)
	string words_[] = {"a","b","aa","ab","ba","bb"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	long long _ = 25LL; 
END

}
// END CUT HERE