Pagini recente » Cod sursa (job #2900095) | Cod sursa (job #2783961) | Cod sursa (job #1832702) | Cod sursa (job #2660474) | Cod sursa (job #2871620)
#include <vector>
#include <fstream>
#define mod 10007
#define prim 3
using namespace std;
ifstream f("abc2.in");
ofstream g("abc2.out");
unsigned int hash1,hashm,p=1,poz,val;
unsigned int sa,sb,i;
string s,d;
vector<unsigned int> v[mod];
bool searchh(unsigned int h)
{
unsigned p=v[h%mod].size();
for(int i=0;i<p;++i)
{
if(v[h%mod][i]==h) return true;
}
return false;
}
int main()
{
f>>s;
sa=s.size();
while(f>>d)
{
sb=d.size();
hash1=0;
for(i=0; i<sb; ++i)
{
hash1=(hash1<<1)+hash1+d[i]-'a';
}
if(!searchh(hash1))
{
v[hash1%mod].push_back(hash1);
}
}
for(i=0;i<sb;++i)
{
hashm=(hashm<<1)+hashm+s[i]-'a';
if(i>0)
{
p=(p<<1)+p;
}
}
if(searchh(hashm)) poz++;
for(i=sb;i<sa;++i)
{
val=hashm-((s[i-sb]-'a')*p);
hashm=(val<<1)+val+s[i]-'a';
if(searchh(hashm)) poz++;
}
g<<poz;
return 0;
}