Pagini recente » Cod sursa (job #2904927) | Cod sursa (job #3252606) | Cod sursa (job #205680) | Cod sursa (job #1776735) | Cod sursa (job #2044110)
#include <fstream>
#include <string.h>
#include <vector>
using namespace std;
ifstream fin ("strmatch.in");
ofstream fout ("strmatch.out");
vector <int> rez;
unsigned long long h_txt[2000002], putere[2000002];
char pattern[2000002], text[2000002];
int cnt;
void rabin_karp()
{
int n = strlen(pattern), m = strlen(text), baza = 71;
unsigned long long h_pat = 0;
for ( int i = n-1; i >= 0; --i )
h_pat = (h_pat * baza + pattern[i]);
for ( int i = m-1; i >= 0; --i )
h_txt[i] = (h_txt[i+1] * baza + text[i]);
putere[0] = 1;
for ( int i = 1; i <= m; ++i )
putere[i] = (putere[i-1] * baza);
for ( int i = n-1; i < m; ++i )
{
if ( h_pat == h_txt[i-n+1] - (h_txt[i+1]*putere[n]) )
{
if ( cnt <= 1000 )
rez.push_back(i-n+1);
cnt++;
}
}
}
int main()
{
fin>>pattern;
fin>>text;
rabin_karp();
fout<<cnt<<'\n';
for ( auto x:rez )
fout<<x<<" ";
}