Pagini recente » Cod sursa (job #2364642) | Cod sursa (job #1940078) | Cod sursa (job #2152542) | Cod sursa (job #1486370) | Cod sursa (job #102199)
Cod sursa(job #102199)
#include <stdio.h>
#include <stdlib.h>
#define MAXTEXT 10000000
#define MAXLEN 20
#define MAXWORDS 50000
int next[MAXWORDS * MAXLEN + 1][3];
int q[MAXWORDS * MAXLEN + 1];
int nodes;
char text[MAXTEXT + 1];
void trie_add(char *word) {
char *p;
int t, ch;
for(p = word, t = 1; *p; p++) {
ch = *p - 'a';
if(!next[t][ch]) next[t][ch] = ++nodes;
t = next[t][ch];
}
q[t] = 1;
}
int main() {
int t[MAXLEN];
char word[MAXLEN + 1], *p;
int i, j, ch, count;
freopen("abc2.in", "rt", stdin);
freopen("abc2.out", "wt", stdout);
gets(text);
nodes = 1;
while(gets(word)) trie_add(word);
memset(t, 0, sizeof(t));
count = 0;
for(p = text; *p; p++) {
ch = *p - 'a';
for(i = j = 0; i < MAXLEN; i++) {
if(!t[i] && !j) {
j = 1;
t[i] = 1;
}
if(t[i]) {
t[i] = next[t[i]][ch];
if(t[i] && q[t[i]]) count++;
}
}
}
printf("%d\n", count);
return 0;
}