Pagini recente » Cod sursa (job #2175068) | Cod sursa (job #2324784) | Cod sursa (job #1890199) | Cod sursa (job #567855) | Cod sursa (job #104437)
Cod sursa(job #104437)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTEXT 10000000
#define MAXLEN 20
#define MAXWORDS 50000
#define MAXNODES (MAXWORDS * MAXLEN + 1)
#define CHARSET 3
int next[MAXNODES][CHARSET], f[MAXNODES], q[MAXNODES], queue[MAXNODES];
char text[MAXTEXT + 1];
void build_failure() {
int i, j, k, a, ql, qr;
ql = qr = 0;
for(i = 0; i < CHARSET; i++) {
if(next[1][i]) {
f[next[1][i]] = 1;
queue[qr++] = next[1][i];
} else {
next[1][i] = 1;
}
}
while(ql < qr) {
i = queue[ql++];
for(j = 0; j < CHARSET; j++) {
a = next[i][j];
if(!a) continue;
queue[qr++] = a;
k = f[i];
while(!next[k][j]) k = f[k];
k = next[k][j];
f[a] = k;
q[a] += q[k];
}
}
}
// Aho-Corasick set matching
int solve() {
char *p;
int t, ch, count;
count = 0;
t = 1;
for(p = text; *p; p++) {
ch = *p - 'a';
while(!next[t][ch]) t = f[t];
t = next[t][ch];
count += q[t];
}
return count;
}
int main() {
char word[MAXLEN + 1], *p;
int t, ch, nodes;
freopen("abc2.in", "rt", stdin);
freopen("abc2.out", "wt", stdout);
gets(text);
nodes = 1;
while(gets(word)) {
t = 1;
for(p = word; *p; p++) {
ch = *p - 'a';
if(!next[t][ch]) next[t][ch] = ++nodes;
t = next[t][ch];
}
q[t] = 1;
}
build_failure();
printf("%d\n", solve());
return 0;
}