Pagini recente » Cod sursa (job #2239846) | Fotbal3 | Cod sursa (job #2683587) | Profil LitaAndrei | Cod sursa (job #2284741)
#include <fstream>
#include <cstring>
#include <vector>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
#define NMAX 2000001
#define MOD1 666013
#define MOD2 666011
#define BASE 150
pair<int,int> operator + (const pair<int,int> &p1, const pair<int,int> &p2)
{
return {p1.first + p2.first, p1.second + p2.second};
}
pair<int,int> operator + (const pair<int,int> &p1, int x)
{
return {p1.first + x, p1.second + x};
}
pair<int,int> operator - (const pair<int,int> &p1, const pair<int,int> &p2)
{
pair<int,int> res {p1.first - p2.first, p1.second - p2.second};
//if(res.first < 0) res.first += MOD1;
//if(res.second < 0) res.second += MOD2;
return res;
}
pair<int,int> operator * (const pair<int,int> &p1, const pair<int,int> &p2)
{
return {p1.first*p2.first % MOD1,
p1.second*p2.second % MOD2};
}
pair<int,int> operator * (const pair<int,int> &p1, int x)
{
return {p1.first*x % MOD1,
p1.second*x % MOD2};
}
char pattern[NMAX], text[NMAX];
int main()
{
fin>>pattern>>text;
pair<int,int> h_pattern{0,0}, h_text{0,0},POW;
int i, len_pattern, len_text;
vector<int> res;
POW = {1, 1};
len_pattern = strlen(pattern);
len_text = strlen(text);
for(i = 0; i < len_pattern; ++i) {
h_pattern = h_pattern * BASE + pattern[i];
h_text = h_text * BASE + text[i];
if(i) POW = POW * BASE;
}
if(h_pattern == h_text)
res.push_back(0);
for(i = len_pattern; i < len_text; ++i) {
h_text = (h_text - POW * text[i-len_pattern]) * BASE + text[i];
if(h_pattern == h_text)
res.push_back(i - len_pattern + 1);
}
fout<<res.size()<<'\n';
int k = min((int)res.size(), 1000);
for(int i = 0; i < k; ++i)
fout<<res[i]<<' ';
return 0;
}