Pagini recente » Rating Tufis Stefan (StefanTufis) | Cod sursa (job #2931132) | Cod sursa (job #2105312) | Cod sursa (job #1920281) | Cod sursa (job #630580)
Cod sursa(job #630580)
#include <stdio.h>
#include <vector>
#include <cstring>
#define MOD 123456
using namespace std;
char s[10000010];
char word[21];
vector<long> table[MOD];
long search_table(long hash) {
long n = table[hash % MOD].size();
for(long i = 0; i < n; i++)
if(table[hash % MOD][i] == hash)
return 1;
return 0;
}
void add_to_table(long hash) {
if (!search_table(hash))
table[hash % MOD].push_back(hash);
}
int main(void) {
freopen("abc2.in","r",stdin);
freopen("abc2.out","w",stdout);
scanf("%s", s);
long i = 0;
long hash = 0;
long len;
while(!feof(stdin)) {
scanf("%s", word);
len = strlen(word);
hash = 0;
for(i = 0; i < len; i++)
hash = (hash * 3 + word[i] - 'a');
add_to_table(hash);
}
long sol = 0;
long offset = 1;
hash = 0;
for(i = 0; i < len; i++) {
hash = hash * 3 + s[i] - 'a';
offset *= 3;
}
offset /= 3;
sol += search_table(hash);
long slen = strlen(s);
for(i = len; i < slen; i++) {
hash = (hash - offset * (s[i - len] - 'a')) * 3 + s[i] - 'a';
sol += search_table(hash);
}
printf("%ld\n", sol);
return 0;
}