Artifact Content
Not logged in

Artifact 71f65eb207acadc5111ef3d876f996bf55ab2ca3


//-------------------------------------------------------------
// Aho-Corasick multi pattern search
//   O(|size-of-patterns|) for construction. O(1) per step.
//
// Verified by
//   - SRM519 Div1 LV2
//-------------------------------------------------------------

template<int NC=26, char BC='a'>
struct AhoCorasick
{
	AhoCorasick( const vector<string>& p ) : link(NC+1) {
		// Create a trie
		for(int i=0; i<p.size(); ++i) {
			AhoCorasick* t = this;
			for(int k=0; k<p[i].size(); t=t->link[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<AhoCorasick*> Q;
		for(int c=0; c<NC; ++c)
			if( link[c] ) {
				Q.push(link[c]);
				link[c]->link[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; c<NC; ++c)
				if( t->link[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<int>& accept() const { return final; }
	~AhoCorasick() { for(int i=0; i<NC; ++i) if(link[i]!=this) delete link[i]; }
private:
	AhoCorasick() : link(NC+1) {}
	vector<AhoCorasick*> link;
	set<int> final;
};