Pagini recente » Cod sursa (job #2557896) | Cod sursa (job #1283837) | Cod sursa (job #1549807) | Cod sursa (job #730726) | Cod sursa (job #1177661)
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class AhoCorasickAutomaton {
public:
static const int NIL = -1;
static const int SIGMA = 26;
AhoCorasickAutomaton(const vector<string> &signatures):
size(1),
start(0),
delta(vector< vector<int> >(1, vector<int>(SIGMA, NIL))),
pi(vector<int>(1, 0)),
order(vector<int>()),
signatureCount(int(signatures.size())),
signatureIndices(vector< vector<int> >(1, vector<int>())) {
for (int i = 0; i < signatureCount; ++i) {
int x = start;
for (int j = 0; j < int(signatures[i].length()); x = delta[x][Encode(signatures[i][j++])])
if (delta[x][Encode(signatures[i][j])] == NIL)
delta[x][Encode(signatures[i][j])] = NewNode();
signatureIndices[x].push_back(i);
}
order.push_back(start);
for (int i = 0; i < int(order.size()); ++i) {
int x = order[i];
for (int symbol = 0; symbol < SIGMA; ++symbol) {
int p = pi[x];
for (; p != start && delta[p][symbol] == NIL; p = pi[p]);
if (delta[p][symbol] != NIL && delta[p][symbol] != delta[x][symbol])
p = delta[p][symbol];
if (delta[x][symbol] == NIL) {
delta[x][symbol] = p;
} else {
pi[delta[x][symbol]] = p;
order.push_back(delta[x][symbol]);
}
}
}
reverse(order.begin(), order.end());
}
vector<int> GetMatches(const string &stringToSearch) {
vector<int> nodeMatches = vector<int>(size, 0);
for (int x = start, i = 0; i < int(stringToSearch.length()); ++i)
++nodeMatches[x = delta[x][Encode(stringToSearch[i])]];
for (int i = 0; order[i] != start; ++i)
nodeMatches[pi[order[i]]] += nodeMatches[order[i]];
vector<int> signatureMatches = vector<int>(signatureCount, 0);
for (int x = 0; x < size; ++x)
for (int i = 0; i < int(signatureIndices[x].size()); ++i)
signatureMatches[signatureIndices[x][i]] += nodeMatches[x];
return signatureMatches;
}
private:
int size, start;
vector< vector<int> > delta;
vector<int> pi, order;
int signatureCount;
vector< vector<int> > signatureIndices;
static int Encode(const char symbol) {
return int(symbol - 'a');
}
int NewNode() {
delta.push_back(vector<int>(SIGMA, NIL));
pi.push_back(start);
signatureIndices.push_back(vector<int>());
return size++;
}
};
int main() {
ifstream in("ahocorasick.in");
ofstream out("ahocorasick.out");
string stringToSearch;
in >> stringToSearch;
int n;
in >> n;
vector<string> signatures = vector<string>(n, "");
for (int i = 0; i < n; ++i)
in >> signatures[i];
AhoCorasickAutomaton automaton = AhoCorasickAutomaton(signatures);
vector<int> matches = automaton.GetMatches(stringToSearch);
for (int i = 0; i < int(matches.size()); ++i)
out << matches[i] << "\n";
in.close();
out.close();
return 0;
}