Pagini recente » Monitorul de evaluare | Cod sursa (job #1857694) | Monitorul de evaluare | Cod sursa (job #2226030) | Cod sursa (job #1364226)
#include <bits/stdc++.h>
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
#define cout g
#define nmax 2000002
#define BASE 75
#define MOD1 999983
#define MOD2 999981
char pattern[nmax],text[nmax];
int hash_pat1,hash_pat2,hash_tx1,hash_tx2,P1,P2;
int n,m,i;
vector <int> sol;
int main()
{
f>>(pattern);
f>>(text);
n=strlen(pattern);
m=strlen(text);
if(n>m){cout<<"0";return 0;}
P1=P2=1;
for(i=0; i<n; ++i)
{
hash_pat1 = (hash_pat1 * BASE + pattern[i] - '0') % MOD1;
hash_pat2 = (hash_pat2 * BASE + pattern[i] - '0') % MOD2;
if(i>0)
{
P1 = P1 * BASE % MOD1;
P2 = P2 * BASE % MOD2;
}
}
for(i=0;i<n;++i)
{
hash_tx1 = (hash_tx1 * BASE + text[i] - '0') % MOD1;
hash_tx2 = (hash_tx2 * BASE + text[i] - '0') % MOD2;
}
if(hash_tx1==hash_pat1 && hash_tx2==hash_pat2) sol.push_back(0);
for(i=n;i<m;++i)
{
hash_tx1 = ((hash_tx1 - (text[i-n] - '0') * P1 % MOD1 + MOD1) * BASE + text[i] - '0') % MOD1;
hash_tx2 = ((hash_tx2 - (text[i-n] - '0') * P2 % MOD2 + MOD2) * BASE + text[i] - '0') % MOD2;
if(hash_tx1==hash_pat1 && hash_tx2==hash_pat2) sol.push_back(i-n+1);
}
cout<<sol.size()<<'\n';
for(i=0;i<min((int)sol.size(),1000);++i) cout<<sol[i]<<' ';
}