Pagini recente » Cod sursa (job #537062) | Cod sursa (job #1785288) | Cod sursa (job #1785294) | Cod sursa (job #537071) | Cod sursa (job #846258)
Cod sursa(job #846258)
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
const int P = 73;
const int MOD1 = 100007;
const int MOD2 = 100021;
vector<int> match;
string A, B;
int N, M, P1, P2, key1, key2;
int main() {
int i, j, hash1, hash2;
f>>A>>B;
N = A.size();
M = B.size();
P1 = P2 = 1;
for(i=0; i<N; ++i) {
key1 = (key1*P + A[i]) % MOD1;
key2 = (key2*P + A[i]) % MOD2;
if(i) {
P1 = (P1 * P) % MOD1;
P2 = (P2 * P) % MOD2;
}
}
if(N > M) {
g<<0<<"\n";
f.close(); g.close();
return 0;
}
hash1 = hash2 = 0;
for(i=0; i<N; ++i) {
hash1 = (hash1*P + B[i]) % MOD1;
hash2 = (hash2*P + B[i]) % MOD2;
}
if(key1 == hash1 && key2 == hash2)
match.push_back(0);
for(i=N; i<M; ++i) {
hash1 = (((hash1 - (B[i-N] * P1) % MOD1) + MOD1) * P + B[i]) % MOD1;
hash2 = (((hash2 - (B[i-N] * P2) % MOD2) + MOD2) * P + B[i]) % MOD2;
if(key1 == hash1 && key2 == hash2)
match.push_back(i-N+1);
}
g<<static_cast<int>(match.size())<<"\n";
for(i=0; i<min(1000, static_cast<int>(match.size())); ++i)
g<<match[i]<<" ";
f.close(); g.close();
return 0;
}