Cod sursa(job #102200)

Utilizator octavOctavian Voicu octav Data 14 noiembrie 2007 04:03:20
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 0.95 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.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;
}