Pagini recente » Cod sursa (job #1400368) | Cod sursa (job #615651) | Cod sursa (job #1136789) | Cod sursa (job #2118669) | Cod sursa (job #629597)
Cod sursa(job #629597)
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int B = 256;
const int P = 666013;
string a,b;
//vector<int> Bp;
//vector<int> BBp;
int Bp = 1, BBp = 1;
vector<int> poz;
int x;
void ReadData()
{
ifstream fin("strmatch.in");
fin>>a>>b;
}
void Baza()
{
int k = a.length();
for(int i=1;i<k;i++)
{
Bp = Bp * B % P;
}
}
void Solve()
{
int k = a.length(),i;
int hashb=0,hasha=0;
for (i=0; i < k; i++)
{
hasha = (hasha * B + a[i]) % P;
}
for (i=0; i < k-1; i++)
{
hashb = (hashb * B + b[i]) % P;
}/*
int ok = 0;
if( hasha == hashb)
{
//poz.push_back(0);
for(i=0;i<k ;i++)
if(a[i]!=b[i])
ok = 1;
if( ok == 0 )
{
poz.push_back(0);
}
}*/
int j = b.length();
int calc[256];
for (int i = 'a'; i < 'z'; ++i)
calc[i] = ((long long) i * Bp) % P;
for (int i = 'A'; i < 'Z'; ++i)
calc[i] = ((long long) i * Bp) % P;
for (int i = '0'; i < '9'; ++i)
calc[i] = ((long long) i * Bp) % P;
for(i=k-1; i < j;++i)
{
hashb = ((P + hashb - calc[b[i - k]]) * B + b[i])%P;
if( hasha == hashb && i >= k - 1)
{
int ok = 0, s=i-k+1;
for(int q = 0; q<k; ++q )
if(a[q] != b[s+q])
ok = 1;
if( !ok)
poz.push_back(s);
}
}
}
void Afisare()
{
ofstream fout("strmatch.out");
int i;
fout<<poz.size()<<"\n";
int j = min(1000, (int) poz.size());
for(i=0;i < j;++i)
fout<<poz[i]<<" ";
}
int main()
{
ReadData();
Baza();
Solve();
Afisare();
return 0;
}