Pagini recente » Cod sursa (job #2576343) | Cod sursa (job #2973554) | Cod sursa (job #2819241) | Cod sursa (job #2555243) | Cod sursa (job #2536213)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
const int MOD1 = 1e9 + 7;
const int MOD2 = 104729;
const int baza = 54;
using namespace std;
ifstream in("strmatch.in");
ofstream out("strmatch.out");
int amod1,amod2,bmod1,bmod2;
int n,m,pown;
int asci(char c){
return c - 'A' + 1;
}
string str(int nr){
string ans;
while(nr){
ans += (nr % baza + 'A' - 1);
nr /= baza;
}
reverse(ans.begin(),ans.end());
return ans;
}
int main()
{
string a,b;
in>>a>>b;
int n = a.size();
int m = b.size();
pown = 1;
for(int i = 1; i <= n - 1; i++)
pown = pown * baza;
for(int i = 0; i < n; i++){
amod1 = (amod1 * baza + asci(a[i])) % MOD1;
amod2 = (amod2 * baza + asci(a[i])) % MOD2;
}
for(int i = 0; i < n; i++){
bmod1 = (bmod1 * baza + asci(b[i])) % MOD1;
bmod2 = (bmod2 * baza + asci(b[i])) % MOD2;
}
vector<int>ans;
if(amod1 == bmod1 && amod2 == bmod2)
ans.push_back(0);
//cout<<bmod1<<" "<<str(bmod1)<<" "<<pown<<endl;
for(int i = n; i < m; i++){
bmod1 = ((bmod1 - asci(b[i - n]) * pown) * baza + asci(b[i]) + MOD1) % MOD1;
bmod2 = ((bmod2 - asci(b[i - n]) * pown) * baza + asci(b[i]) + MOD2) % MOD2;
if(amod1 == bmod1 && amod2 == bmod2)
ans.push_back(i - n + 1);
}
out<<ans.size()<<"\n";
for(auto i : ans)
out<<i<<" ";
return 0;
}