Pagini recente » Cod sursa (job #786971) | Cod sursa (job #381867) | Cod sursa (job #1742272) | Cod sursa (job #1940261) | Cod sursa (job #2976544)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
const int b = 65;
const int MOD = 100007;
string p , t;
vector <int> poz;
int trans( char x ){
if( x >= 'a' ){
return x - 'a' + 1;
}
if( x >= 'A'){
return 26 + x - 'A' + 1;
}
return 52 + x - '0' + 1;
}
int main(){
fin >> p >> t;
int n = t.size();
int m = p.size();
int hashp = 0 , hasht = 0 , h = 1;
for(int i = 0 ; i < m - 1 ; i++){
h = ( h * b )%MOD;
}
for(int i = 0 ; i < m ; i++){
hashp = ((hashp*b)%MOD + trans(p[i]))%MOD;
hasht = ((hasht*b)%MOD + trans(t[i]))%MOD;
}
if(hashp == hasht) poz.push_back(0);
for(int i = 1 ; ( i + m - 1 ) < n ; i++){
hasht = (((hasht - (trans(t[i-1])*h)%MOD) * b)%MOD + trans(t[( i + m - 1 )]))%MOD;
if(hasht == hashp) poz.push_back(i);
}
fout << poz.size()<< '\n';
for( auto it : poz ){
fout << it << ' ';
}
return 0;
}