Pagini recente » Cod sursa (job #667984) | Cod sursa (job #3150614) | Cod sursa (job #3127788) | Cod sursa (job #931814) | Cod sursa (job #168459)
Cod sursa(job #168459)
#include <fstream>
#include <map>
#include <algorithm>
using namespace std;
const char iname[] = "abc2.in";
const char oname[] = "abc2.out";
#define FOR(i, a, b) for (int i = (a); i < (b); ++ i)
#define MAXN 10000001
char T[MAXN];
map <unsigned int, bool> hash;
int main(void)
{
ifstream fin(iname);
int n;
int m = 0;
char word[21];
unsigned int h;
int ret = 0;
fin.getline(T, MAXN);
n = (int)strlen(T);
fin.getline(word, 21);
m = (int)strlen(word);
while (*word)
{
h = 0;
for (char *p = word; *p; ++ p)
h = h * 3 + (*p - 'a');
hash[h] = true;
fin.getline(word, 21);
}
unsigned int bpm = 1;
FOR (i, 0, m - 1)
bpm *= 3;
h = 0;
FOR (i, 0, m)
h = h * 3 + (T[i] - 'a');
FOR (i, 0, n-m+1)
{
if (hash.find(h) != hash.end())
ret ++;
if (i + m < n)
h = (h - bpm * (T[i] - 'a')) * 3 + (T[i + m] - 'a');
}
ofstream fout(oname);
fout << ret <<'\n';
fin.close(), fout.close();
return 0;
}