Pagini recente » Cod sursa (job #169741) | Cod sursa (job #2578839) | Cod sursa (job #1514334) | Cod sursa (job #2702053) | Cod sursa (job #1968069)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in("strmatch.in");
ofstream out("strmatch.out");
#define pb push_back
typedef long long ll;
const int NMax = 2e6 + 5;
const ll base = 73;
const ll mod1 = 100003;
const ll mod2 = 100007;
vector<int> sol;
int main() {
string str,patt,temp;
in>>temp;
patt = '\0' + temp;;
in>>temp;
str = '\0' + temp;
ll N = patt.size() - 1,M = str.size() - 1,
pattHash1 = 0,pattHash2 = 0,strHash1 = 0,strHash2 = 0,pw1 = 1,pw2 = 1;
for (int i=1;i <= N;++i) {
pattHash1 = (pattHash1 * base + patt[i] * base) % mod1;
pattHash2 = (pattHash2 * base + patt[i] * base) % mod2;
strHash1 = (strHash1 * base + str[i] * base) % mod1;
strHash2 = (strHash2 * base + str[i] * base) % mod2;
pw1 = (pw1 * base) % mod1;
pw2 = (pw2 * base) % mod2;
}
if (pattHash1 == strHash1 && pattHash2 == strHash2) {
sol.pb(0);
}
for (int i=N+1;i <= M;++i) {
strHash1 = ((strHash1 - (str[i-N] * pw1) % mod1 + mod1) * base + str[i]*base) % mod1;
strHash2 = ((strHash2 - (str[i-N] * pw2) % mod2 + mod2) * base + str[i]*base) % mod2;
if (pattHash1 == strHash1 && pattHash2 == strHash2) {
sol.pb(i-N);
}
}
out<<sol.size()<<'\n';
int lim = (1000 < sol.size()) ? 1000 : sol.size();
for (int i=0;i<lim;++i) {
out<<sol[i]<<' ';
}
in.close();out.close();
return 0;
}