Pagini recente » Cod sursa (job #2107948) | Cod sursa (job #488850) | Cod sursa (job #1580135) | Cod sursa (job #2346871) | Cod sursa (job #2291831)
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#define MOD 666013
using namespace std;
vector <int> h[MOD];
inline int M(int x)
{
if(x < MOD)
return x;
return x%MOD;
}
inline int val(string S,int dim)
{
int x = 0, p3 = 1;
for (int i = dim - 1; i > -1; --i)
{
x += (S[i] - 'a') * p3;
p3 *= 3;
}
return x;
}
inline void Push(int x)
{
int p = M(x);
for (int i = 0; i < h[p].size(); ++i)
if(h[p][i] == x)
return;
h[p].push_back(x);
}
inline bool Search(int x)
{
int p = M(x);
for (int i = 0; i < h[p].size(); ++i)
if(h[p][i] == x)
return true;
return false;
}
int main()
{
ifstream fin ("abc2.in");
ofstream fout ("abc2.out");
string s,curr;
int x,i,n,k,cnt = 0,P = 1;
fin >> s >> curr;
k = curr.size();
n = s.size();
for (i = 1; i < k; ++i)
P *=3;
x = val(curr,k);
Push(x);
while(fin >> curr)
{
x = val(curr,k);
Push(x);
}
x = val(s,k);
if(Search(x))
++cnt;
for(i = k; i < n; ++i)
{
x -= (s[i - k] - 'a') * P;
x = x * 3 + s[i] - 'a';
if(Search(x))
{
for(int j = i - k + 1; j <= i; ++j)
cout<<s[j];
cout<< '\n';
++cnt;
}
}
fout << cnt;
return 0;
}