ADDED SRM/519-U/1A.cpp Index: SRM/519-U/1A.cpp ================================================================== --- SRM/519-U/1A.cpp +++ SRM/519-U/1A.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class BinaryCards { public: + long long largestNumber(long long A_, long long B_) + { + unsigned long long A = A_; + unsigned long long B = B_; + string sa; + for(int i=0; (1ULL<>i)&1)) + sa; + string sb; + for(int i=0; (1ULL<>i)&1)) + sb; + sa = string(sb.size()-sa.size(), '0') + sa; + + string sc; + for(int i=0; i-custom +// Powered by CodeProcessor ADDED SRM/519-U/1B.cpp Index: SRM/519-U/1B.cpp ================================================================== --- SRM/519-U/1B.cpp +++ SRM/519-U/1B.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +template +struct AhoCorasick +{ + AhoCorasick( const vector& p ) : link(NC+1) { + // Create a trie + for(int i=0; ilink[p[i][k++]-BC]) + if(!t->link[p[i][k]-BC]) + t->link[p[i][k]-BC]=new AhoCorasick; + t->final.insert(i); + } + + // Do BFS and draw failure links, and prepare for substring pattern + queue Q; + for(int c=0; clink[NC] = this; // "c"'s suffix is "" + link[c]->final.insert(final.begin(), final.end()); + } + else + link[c] = this; + while( !Q.empty() ) { + AhoCorasick* t=Q.front(); Q.pop(); + for(int c=0; clink[c] ) { + Q.push(t->link[c]); + AhoCorasick* r = t->link[NC]; // "r" is suffix of "t"... + while( !r->link[c] ) + r = r->link[NC]; + t->link[c]->link[NC] = r->link[c]; // then "rc" is suffix of "tc" + t->link[c]->final.insert(r->link[c]->final.begin(), r->link[c]->final.end()); + } + } + } + const AhoCorasick* start() const { return this; } + const AhoCorasick* next(char c) const { + const AhoCorasick* t = this; + while( !t->link[c-BC] ) + t = t->link[NC]; + return t->link[c-BC]; + } + const set& accept() const { return final; } + ~AhoCorasick() { for(int i=0; i link; + set final; +}; + +static const int MODVAL = 1000000009; // must be prime for op/ +struct mint +{ + int val; + mint():val(0){} + mint(int x):val(x%MODVAL) {} +}; +mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } + +int bitcnt(LL x) +{ + int c = 0; + for(; x; x>>=1) + c += x&1; + return c; +} + +class RequiredSubstrings { public: + int solve(vector words, int C, int L) + { + AhoCorasick<> ac(words); + return rec(L, C, 0, ac.start()).val; + } + + map,const AhoCorasick<>*>, mint> memo; + mint rec(int rest, const int C, const int found, const AhoCorasick<>* ac) + { + if( rest == 0 ) + return (bitcnt(found)==C ? 1 : 0); + pair,const AhoCorasick*> key(make_pair(rest, found), ac); + if( memo.count(key) ) + return memo[key]; + + mint sum = 0; + for(char c='a'; c<='z'; ++c) + { + const AhoCorasick<>* t = ac->next(c); + int f = found; + for(set::const_iterator it=t->accept().begin(); it!=t->accept().end(); ++it) + f |= (1 << *it); + sum += rec(rest-1, C, f, t); + } + return memo[key] = sum; + } +}; + + + +// Powered by FileEdit +// Powered by TZTester 1.01 [25-Feb-2003] : -custom +// Powered by CodeProcessor + ADDED lib/dataStructure/AhoCorasick.cpp Index: lib/dataStructure/AhoCorasick.cpp ================================================================== --- lib/dataStructure/AhoCorasick.cpp +++ lib/dataStructure/AhoCorasick.cpp @@ -0,0 +1,58 @@ +//------------------------------------------------------------- +// Aho-Corasick multi pattern search +// O(|size-of-patterns|) for construction. O(1) per step. +// +// Verified by +// - SRM519 Div1 LV2 +//------------------------------------------------------------- + +template +struct AhoCorasick +{ + AhoCorasick( const vector& p ) : link(NC+1) { + // Create a trie + for(int i=0; ilink[p[i][k++]-BC]) + if(!t->link[p[i][k]-BC]) + t->link[p[i][k]-BC]=new AhoCorasick; + t->final.insert(i); + } + + // Do BFS and draw failure links, and prepare for substring pattern + queue Q; + for(int c=0; clink[NC] = this; // "c"'s suffix is "" + link[c]->final.insert(final.begin(), final.end()); + } + else + link[c] = this; + while( !Q.empty() ) { + AhoCorasick* t=Q.front(); Q.pop(); + for(int c=0; clink[c] ) { + Q.push(t->link[c]); + AhoCorasick* r = t->link[NC]; // "r" is suffix of "t"... + while( !r->link[c] ) + r = r->link[NC]; + t->link[c]->link[NC] = r->link[c]; // then "rc" is suffix of "tc" + t->link[c]->final.insert(r->link[c]->final.begin(), r->link[c]->final.end()); + } + } + } + const AhoCorasick* start() const { return this; } + const AhoCorasick* next(char c) const { + const AhoCorasick* t = this; + while( !t->link[c-BC] ) + t = t->link[NC]; + return t->link[c-BC]; + } + const set& accept() const { return final; } + ~AhoCorasick() { for(int i=0; i link; + set final; +};