Pagini recente » Cod sursa (job #106971) | Cod sursa (job #410097) | Cod sursa (job #2313648) | Cod sursa (job #386751) | Cod sursa (job #2102433)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in("abc2.in");
ofstream out("abc2.out");
const int mod = 30000;
//unordered_map <int, bool> marcat;
vector <int> hsh[mod + 5];
string text;
string cuv;
void push(int x)
{
int p = x % mod;
hsh[p].push_back(x);
}
bool searchfor(int x)
{
int p = x % mod;
for(auto it : hsh[p])
if(it == x)
return 1;
return 0;
}
void codif(string s)
{
/*
int p1 = 0;
int p2 = 0;
for(auto it : s)
{
p1 = (p1 * 3 + it - 'a') % mod1;
p2 = (p2 * 3 + it - 'a') % mod2;
}
*/
//marcat[make_pair(p1, p2)] = 1;
int p = 0;
for(auto it : s)
p = p * 3 + it - 'a';
push(p);
}
int main()
{
in >> text;
while(in >> cuv)
codif(cuv);
int lg = cuv.size();
int put3 = 1;
int a = 0;
for(int i = 0; i < lg; i++)
{
a = a * 3 + text[i] - 'a';
put3 = put3 * 3;
}
put3 = put3 / 3;
int nr = 0;
for(unsigned int i = lg; i < text.size(); i++)
{
//cerr << a << "\n";
if(searchfor(a) == 1)
nr++;
a = a - put3 * (text[i - lg] - 'a');
a = a * 3 + text[i] - 'a';
}
if(searchfor(a) == 1)
nr++;
out << nr << "\n";
return 0;
}