Pagini recente » Borderou de evaluare (job #1503670) | Cod sursa (job #2831923) | Cod sursa (job #204198) | Cod sursa (job #548382) | Cod sursa (job #3280035)
#include <fstream>
#include <vector>
using namespace std;
ifstream cin ("strmatch.in");
ofstream cout ("strmatch.out");
const int mod1=665993,mod2=666013;
const int base1=271,base2=277;
long long powers1[15],powers2[15];
string a;
vector <int> answers;
void powermaker()
{
long long check1=1,check2=1;
powers1[0]=1;
powers2[0]=1;
for(int i=1;i<=9;i++)
{
powers1[i]=check1;
check1*=base1;
check1%=mod1;
powers2[i]=check2;
check2*=base2;
check2%=mod2;
}
}
vector <int> v1,v2;
int hashstring(const string &s,int mod,long long powers[15])
{
long long newx=0;
int p=1;
for(int i=s.size()-1;i>=0;i--)
{
newx+=1LL*powers[p]*(s[i]);
newx%=mod;
cout << newx << endl;
p++;
}
return newx;
}
int tn(int tbh,char toadd,char toremove,int mod,int mostSignificantPower, int base) //tonext
{
tbh-=toremove*mostSignificantPower;
tbh+=mod;
tbh%=mod;
tbh*=base;
tbh%=mod;
tbh*=toadd;
tbh%=mod;
return tbh;
}
int main()
{ v1.resize(mod1+5);
v2.resize(mod2+5);
powermaker();
for(int i=1;i<=10;i++)
cout<<powers1[i]<<' ';
cout<<endl;
for(int i=1;i<=10;i++)
cout<<powers2[i]<<' ';
string b;
cin>>a;
int ah1=hashstring(a,mod1,powers1); //ahashed1
int ah2=hashstring(a,mod2,powers2); //ahashed2
cin>>b;
string tbh1,tbh2;//tbh=tobehashed
int tho1,tho2;//the hashed one
tho1=hashstring(tbh1.substr(0, a.size()),mod1,powers1);
tho2=hashstring(tbh2.substr(0, a.size()),mod2,powers2);
cout<<tho1<<' '<<tho2<<endl;
v1[tho1]++;
v2[tho2]++;
string s;
cin>>s;
int numberofmatches=0;
for(int i=a.size();i<b.size();i++)
{
tho1=tn(tho1,b[i],b[i-a.size()-1],mod1,powers1[a.size()],base1);
tho2=tn(tho2,b[i],b[i-a.size()-1],mod2,powers2[a.size()],base2);
if(ah1==tho1 and ah2==tho2){
numberofmatches++;
answers.push_back(i-a.size()-1);
}
}
cout<<numberofmatches<<'\n';
int size=answers.size();
for(int i=0;i<min(size,999);i++)
cout<<answers[i]<<' ';
return 0;
}