Pagini recente » Cod sursa (job #1242756) | Cod sursa (job #1586570) | Cod sursa (job #2080864) | Cod sursa (job #3216948) | Cod sursa (job #629621)
Cod sursa(job #629621)
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int B = 67;
const int P = 666013;
string a,b;
int Bp = 1, BBp = 1;
vector<int> poz;
int x;
int calc[256];
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;
}
k--;
for (i=0; i < k; ++i)
{
hashb = (hashb * B + b[i]) % P;
}
k++;
int j = b.length();
for (int i = 0; i <256; ++i)
calc[i] = (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");
freopen("strmatch.out","w",stdout);
int i = (int)poz.size();
//fout<<i<<"\n";
printf("%d\n",i);
int j = min(1000, i);
for(i=0;i < j ;++i)
//fout<<poz[i]<<" ";
printf("%d ",poz[i]);
//fout<<"\n";
printf("\n");
}
int main()
{
ReadData();
Baza();
Solve();
Afisare();
return 0;
}