28a087506a 2011-09-21 kinaba: #include <iostream> 28a087506a 2011-09-21 kinaba: #include <sstream> 28a087506a 2011-09-21 kinaba: #include <iomanip> 28a087506a 2011-09-21 kinaba: #include <vector> 28a087506a 2011-09-21 kinaba: #include <string> 28a087506a 2011-09-21 kinaba: #include <map> 28a087506a 2011-09-21 kinaba: #include <set> 28a087506a 2011-09-21 kinaba: #include <algorithm> 28a087506a 2011-09-21 kinaba: #include <numeric> 28a087506a 2011-09-21 kinaba: #include <iterator> 28a087506a 2011-09-21 kinaba: #include <functional> 28a087506a 2011-09-21 kinaba: #include <complex> 28a087506a 2011-09-21 kinaba: #include <queue> 28a087506a 2011-09-21 kinaba: #include <stack> 28a087506a 2011-09-21 kinaba: #include <cmath> 28a087506a 2011-09-21 kinaba: #include <cassert> 28a087506a 2011-09-21 kinaba: #include <cstring> 28a087506a 2011-09-21 kinaba: using namespace std; 28a087506a 2011-09-21 kinaba: typedef long long LL; 28a087506a 2011-09-21 kinaba: typedef complex<double> CMP; 28a087506a 2011-09-21 kinaba: 28a087506a 2011-09-21 kinaba: template<int NC=26, char BC='a'> 28a087506a 2011-09-21 kinaba: struct AhoCorasick 28a087506a 2011-09-21 kinaba: { 28a087506a 2011-09-21 kinaba: AhoCorasick( const vector<string>& p ) : link(NC+1) { 28a087506a 2011-09-21 kinaba: // Create a trie 28a087506a 2011-09-21 kinaba: for(int i=0; i<p.size(); ++i) { 28a087506a 2011-09-21 kinaba: AhoCorasick* t = this; 28a087506a 2011-09-21 kinaba: for(int k=0; k<p[i].size(); t=t->link[p[i][k++]-BC]) 28a087506a 2011-09-21 kinaba: if(!t->link[p[i][k]-BC]) 28a087506a 2011-09-21 kinaba: t->link[p[i][k]-BC]=new AhoCorasick; 28a087506a 2011-09-21 kinaba: t->final.insert(i); 28a087506a 2011-09-21 kinaba: } 28a087506a 2011-09-21 kinaba: 28a087506a 2011-09-21 kinaba: // Do BFS and draw failure links, and prepare for substring pattern 28a087506a 2011-09-21 kinaba: queue<AhoCorasick*> Q; 28a087506a 2011-09-21 kinaba: for(int c=0; c<NC; ++c) 28a087506a 2011-09-21 kinaba: if( link[c] ) { 28a087506a 2011-09-21 kinaba: Q.push(link[c]); 28a087506a 2011-09-21 kinaba: link[c]->link[NC] = this; // "c"'s suffix is "" 28a087506a 2011-09-21 kinaba: link[c]->final.insert(final.begin(), final.end()); 28a087506a 2011-09-21 kinaba: } 28a087506a 2011-09-21 kinaba: else 28a087506a 2011-09-21 kinaba: link[c] = this; 28a087506a 2011-09-21 kinaba: while( !Q.empty() ) { 28a087506a 2011-09-21 kinaba: AhoCorasick* t=Q.front(); Q.pop(); 28a087506a 2011-09-21 kinaba: for(int c=0; c<NC; ++c) 28a087506a 2011-09-21 kinaba: if( t->link[c] ) { 28a087506a 2011-09-21 kinaba: Q.push(t->link[c]); 28a087506a 2011-09-21 kinaba: AhoCorasick* r = t->link[NC]; // "r" is suffix of "t"... 28a087506a 2011-09-21 kinaba: while( !r->link[c] ) 28a087506a 2011-09-21 kinaba: r = r->link[NC]; 28a087506a 2011-09-21 kinaba: t->link[c]->link[NC] = r->link[c]; // then "rc" is suffix of "tc" 28a087506a 2011-09-21 kinaba: t->link[c]->final.insert(r->link[c]->final.begin(), r->link[c]->final.end()); 28a087506a 2011-09-21 kinaba: } 28a087506a 2011-09-21 kinaba: } 28a087506a 2011-09-21 kinaba: } 28a087506a 2011-09-21 kinaba: const AhoCorasick* start() const { return this; } 28a087506a 2011-09-21 kinaba: const AhoCorasick* next(char c) const { 28a087506a 2011-09-21 kinaba: const AhoCorasick* t = this; 28a087506a 2011-09-21 kinaba: while( !t->link[c-BC] ) 28a087506a 2011-09-21 kinaba: t = t->link[NC]; 28a087506a 2011-09-21 kinaba: return t->link[c-BC]; 28a087506a 2011-09-21 kinaba: } 28a087506a 2011-09-21 kinaba: const set<int>& accept() const { return final; } 28a087506a 2011-09-21 kinaba: ~AhoCorasick() { for(int i=0; i<NC; ++i) if(link[i]!=this) delete link[i]; } 28a087506a 2011-09-21 kinaba: private: 28a087506a 2011-09-21 kinaba: AhoCorasick() : link(NC+1) {} 28a087506a 2011-09-21 kinaba: vector<AhoCorasick*> link; 28a087506a 2011-09-21 kinaba: set<int> final; 28a087506a 2011-09-21 kinaba: }; 28a087506a 2011-09-21 kinaba: 28a087506a 2011-09-21 kinaba: static const int MODVAL = 1000000009; // must be prime for op/ 28a087506a 2011-09-21 kinaba: struct mint 28a087506a 2011-09-21 kinaba: { 28a087506a 2011-09-21 kinaba: int val; 28a087506a 2011-09-21 kinaba: mint():val(0){} 28a087506a 2011-09-21 kinaba: mint(int x):val(x%MODVAL) {} 28a087506a 2011-09-21 kinaba: }; 28a087506a 2011-09-21 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 28a087506a 2011-09-21 kinaba: 28a087506a 2011-09-21 kinaba: int bitcnt(LL x) 28a087506a 2011-09-21 kinaba: { 28a087506a 2011-09-21 kinaba: int c = 0; 28a087506a 2011-09-21 kinaba: for(; x; x>>=1) 28a087506a 2011-09-21 kinaba: c += x&1; 28a087506a 2011-09-21 kinaba: return c; 28a087506a 2011-09-21 kinaba: } 28a087506a 2011-09-21 kinaba: 28a087506a 2011-09-21 kinaba: class RequiredSubstrings { public: 28a087506a 2011-09-21 kinaba: int solve(vector <string> words, int C, int L) 28a087506a 2011-09-21 kinaba: { 28a087506a 2011-09-21 kinaba: AhoCorasick<> ac(words); 28a087506a 2011-09-21 kinaba: return rec(L, C, 0, ac.start()).val; 28a087506a 2011-09-21 kinaba: } 28a087506a 2011-09-21 kinaba: 28a087506a 2011-09-21 kinaba: map<pair<pair<int,int>,const AhoCorasick<>*>, mint> memo; 28a087506a 2011-09-21 kinaba: mint rec(int rest, const int C, const int found, const AhoCorasick<>* ac) 28a087506a 2011-09-21 kinaba: { 28a087506a 2011-09-21 kinaba: if( rest == 0 ) 28a087506a 2011-09-21 kinaba: return (bitcnt(found)==C ? 1 : 0); 28a087506a 2011-09-21 kinaba: pair<pair<int,int>,const AhoCorasick*> key(make_pair(rest, found), ac); 28a087506a 2011-09-21 kinaba: if( memo.count(key) ) 28a087506a 2011-09-21 kinaba: return memo[key]; 28a087506a 2011-09-21 kinaba: 28a087506a 2011-09-21 kinaba: mint sum = 0; 28a087506a 2011-09-21 kinaba: for(char c='a'; c<='z'; ++c) 28a087506a 2011-09-21 kinaba: { 28a087506a 2011-09-21 kinaba: const AhoCorasick<>* t = ac->next(c); 28a087506a 2011-09-21 kinaba: int f = found; 28a087506a 2011-09-21 kinaba: for(set<int>::const_iterator it=t->accept().begin(); it!=t->accept().end(); ++it) 28a087506a 2011-09-21 kinaba: f |= (1 << *it); 28a087506a 2011-09-21 kinaba: sum += rec(rest-1, C, f, t); 28a087506a 2011-09-21 kinaba: } 28a087506a 2011-09-21 kinaba: return memo[key] = sum; 28a087506a 2011-09-21 kinaba: } 28a087506a 2011-09-21 kinaba: }; 28a087506a 2011-09-21 kinaba: 28a087506a 2011-09-21 kinaba: 28a087506a 2011-09-21 kinaba: 28a087506a 2011-09-21 kinaba: // Powered by FileEdit 28a087506a 2011-09-21 kinaba: // Powered by TZTester 1.01 [25-Feb-2003] : <cafelier&naoya_t>-custom 28a087506a 2011-09-21 kinaba: // Powered by CodeProcessor 28a087506a 2011-09-21 kinaba: