Pagini recente » Cod sursa (job #597191) | Cod sursa (job #1490630) | Cod sursa (job #459062) | Cod sursa (job #709795) | Cod sursa (job #878142)
Cod sursa(job #878142)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
void redirectInput(const char *in, const char *out)
{
freopen(in, "r", stdin);
//freopen(out, "w", stdout);
}
int hash(string &s, int start, int l)
{
int x = 1;
int base = 101;
int hash = 0;
for(int i = start; i < l; ++i)
{
hash += s[i] * x;
x *= base;
}
return hash;
}
int hash(string &s, int start, int l, int prevHash)
{
return prevHash - s[start - 1] + s[start + l];
}
void solve()
{
char *a = (char*) calloc (2000000, sizeof(char));
char *b = (char*) calloc (2000000, sizeof(char));
scanf("%s %s", a, b);
int n = strlen(a);
int m = strlen(b);
/*
if( m < n )
return;
int hs = hash(a, 0, n);
int h = hash(b, 0, n);
if( h == hs && strncmp(a.c_str(), b.c_str(), n) == 0)
{
cout << 0 << endl;
}
vector<int> matches;
for(int i = 1; i < m - n; ++i)
{
h = hash(b, i, n, h);
if(h == hs)
{
if( strncmp(a.c_str(), b.c_str() + i, n) == 0)
matches.push_back(i);
}
}
int n_to_print = (matches.size() > 1000) ? 1000 : matches.size();
for(int i = 0; i < n_to_print; ++i)
cout << matches[i] << endl;
*/
}
int main(int argc, char *argv[])
{
if(argc != 2)
redirectInput("strmatch.in", "strmatch.out");
else
redirectInput(argv[1], "strmatch.out");
solve();
return 0;
}