Pagini recente » Cod sursa (job #1295430) | Cod sursa (job #544976) | Cod sursa (job #1350708) | Cod sursa (job #2148869) | Cod sursa (job #106749)
Cod sursa(job #106749)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTEXT 10000000
#define MAXLEN 20
#define MAXWORDS 50000
#define HASHSIZE (1 << 20)
typedef unsigned long long CODE;
CODE hashcode[HASHSIZE];
CODE hashmask[HASHSIZE];
CODE maxmask;
char text[MAXTEXT + 1];
int hash_code(CODE code, CODE mask) {
return (code & 0xfffff) ^ ((code >> 25) & 0xfffff) ^ (mask & 0xfffff);
}
void hash_add(char *word) {
CODE code, mask;
char *p;
int h;
code = 0;
mask = 0;
for(p = word; *p; p++) {
code <<= 2;
code |= *p - 'a';
mask <<= 2;
mask |= 3;
}
if(maxmask < mask) maxmask = mask;
h = hash_code(code, mask);
while(hashmask[h] && (hashcode[h] != code || hashmask[h] != mask)) h = (h + 1) & (HASHSIZE - 1);
if(hashmask[h]) return;
hashcode[h] = code;
hashmask[h] = mask;
}
int hash_find(CODE code, CODE mask) {
int h;
code &= mask;
h = hash_code(code, mask);
while(hashmask[h] && (hashcode[h] != code || hashmask[h] != mask)) h = (h + 1) & (HASHSIZE - 1);
return hashmask[h] ? 1 : 0;
}
int main() {
CODE code, mask;
char word[MAXLEN + 1], *p;
int count;
freopen("abc2.in", "rt", stdin);
freopen("abc2.out", "wt", stdout);
gets(text);
while(gets(word)) hash_add(word);
count = 0;
code = 0;
for(p = text; *p; p++) {
code <<= 2;
code |= *p - 'a';
for(mask = maxmask; mask; mask >>= 2) {
if(hash_find(code, mask)) count++;
}
}
printf("%d\n", count);
return 0;
}