Pagini recente » Istoria paginii runda/lot_alba/clasament | Cod sursa (job #531943) | Cod sursa (job #1140314) | Istoria paginii runda/kikis_delivery_service | Cod sursa (job #1230548)
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>
using namespace std;
const char infile[] = "strmatch.in";
const char outfile[] = "strmatch.out";
ifstream fin(infile);
ofstream fout(outfile);
const int MAXN = 2000005;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
string A, B, S;
int N, M, Z[2*MAXN];
inline int expand(int x, int y) {
int cnt = 0;
for(int i = x, j = y ; j < S.size() ; ++ i, ++ j) {
if(S[i] != S[j])
return cnt;
++ cnt;
}
return cnt;
}
inline void preprocessZ() {
int L = -1, R = -1;
S = A + B;
Z[0] = S.size();
for(int i = 1 ; i < S.size() ; ++ i)
if(i > R) {
Z[i] = expand(0, i);
if(Z[i] != 0)
L = i, R = i + Z[i] - 1;
}
else {
int k = i - L, lungA = R - L + 1, lungB = R - i + 1;
if(Z[k] < lungB) {
Z[i] = Z[k];
continue;
}
Z[i] = lungB + expand(lungB, R + 1);
if(i + Z[i] - 1 > R)
L = i, R = i + Z[i] - 1;
}
}
int main() {
fin >> A >> B;
preprocessZ();
vector <int> matches;
for(int i = A.size() ; i < S.size() ; ++ i) {
cerr << Z[i] << ' ';
if(Z[i] >= A.size())
matches.push_back(i - A.size());
}
fout << matches.size() << '\n';
for(int i = 0 ;i < min(1000, matches.size()) ; ++ i)
fout << matches[i] << ' ';
fin.close();
fout.close();
return 0;
}