Pagini recente » Cod sursa (job #1686730) | Istoria paginii runda/pregatire_ori_10/clasament | Cod sursa (job #907637) | Istoria paginii warm-up-2019/solutii/shoturi | Cod sursa (job #2483460)
#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>
using namespace std;
ifstream in("strmatch.in");
ofstream out("strmatch.out");
const int DIM = 2000005;
const long long cst = 100007;
const int nr = 2;
char a[DIM];
char b[DIM];
long long power[DIM];
int rez[DIM];
int main()
{
/*functia hash se face cu puterile nr de litere posibile din vector in cazul acesta sunt nr de litere mici si mari din alfabet englez adica 52 */
in >> a;
in >> b;
int K = 0;
power[0] = 1;
int A = strlen(a);
for(int i = 1;i <= A;i++)
{
power[i] = (power[i - 1] * nr )% cst;
}
int B = strlen(b);
if(A > B)
{out << 0;
return 0;
}
long long S = 0;
long long val = 0;
for(int i = 0;i < A;i++)
{
val += (power[A - i - 1] * (int(a[i]) - 46)) % cst;
val %= cst;
S += (power[A - i - 1] *(int (b[i]) - 46)) % cst;
S %= cst;
}
if(S == val)
{K++;
rez[K] = 0;
}
for(int i = 1; i <= B - A;i++)
{
S = (((S - power[A - 1] * (int(b[i-1]) - 46)) % cst * nr )% cst + (int(b[i + A - 1]) - 46)) % cst;
if(S == val)
{
K++;
rez[K] = i;
}
}
out << K<<"\n";
/* normal K ,in cazul asta e restrictie de 1000 de afisari*/
for(int i = 1; i <= min(1000,K); i++)
out << rez[i] <<" ";
return 0;
}