Pagini recente » Profil stanioan | Monitorul de evaluare | Cod sursa (job #1457155) | Cod sursa (job #3336118) | Cod sursa (job #1457154)
#include <fstream>
#include <vector>
#include <cmath>
#include <cstring>
using namespace std;
#define base 256 // number of characters
#define mod_prime 179422921 // big-enough prime number
int main(int argc, char **argv)
{
// INPUT
ifstream indata("strmatch.in");
char A[2000001], B[2000001];
indata >> A >> B;
indata.close();
// PATTERN MATCHING
vector<int> positions;
int m = strlen(A), n = strlen(B);
// calculate hash for pattern
// and for first substring
int baseToPow = 1;
int hashA = 0, hashB_last = 0;
for (int i = 0; i < m; i++) {
baseToPow *= base;
hashA = (hashA * base + A[i]) ;
hashB_last = (hashB_last * base + B[i]) ;
}
baseToPow /= base;
// check that the hashes match
for (int i = 0; i + m <= n; i++) {
if (hashA == hashB_last) {
// make sure it's not a collision
int q = 0;
while(A[q] == B[i + q]) {
q++;
}
if (q == m) {
// it's not a collision
positions.push_back(i);
}
}
hashB_last = abs(((hashB_last - B[i] * baseToPow) * base
+ B[i + m]));
}
// OUTPUT
ofstream outdata("strmatch.out");
outdata << positions.size() << "\n";
for (size_t i = 0; i < positions.size() && i < 1000; i++) {
outdata << positions[i] << " ";
}
outdata.close();
return 0;
}