Cod sursa(job #1290037)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 10 decembrie 2014 19:03:33
Problema Potrivirea sirurilor Scor 16
Compilator cpp Status done
Runda Arhiva educationala Marime 2.86 kb
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "strmatch";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif // HOME

typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;

const int INF = (1LL << 31) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int MOD = (int)(1e9) + 7;

const int NMAX = 50000 + 5;
const int MMAX = 100000 + 5;
const int KMAX = 100000 + 5;
const int PMAX = 100000 + 5;
const int LMAX = 2000000 + 5;
const int VMAX = 100000 + 5;

int LA, LB;
char A[LMAX];
char B[LMAX];
int Z[LMAX];
int nrMatches;
int matches[1005];

void z_algo() {
    int L, R, i, k;

    L = R = -1;

    for(i = 2; i <= LA; i++)
        if(i > R) {
            for(L = R = i, k = 1; R <= LA && A[k] == A[R]; k++, R++);
            R--;
            Z[i] = R - L + 1;
        } else if(i <= R) {
            Z[i] = Z[i - L + 1];
            if(i + Z[i] - 1 >= R) {
                for(L = i, R = i + Z[i] - 1, k = Z[i]; R <= LA && A[k] == A[R]; k++, R++);
                R--;
                Z[i] = R - L + 1;
            }
        }
}

void matching() {
    int L, R, i, k, len;

    L = R = -1;

    for(i = 1; i <= LB; i++) {
        if(i > R) {
            for(L = R = i, k = 1; k <= LA && R <= LB && A[k] == B[R]; k++, R++);
            R--;
            len = R - L + 1;
        } else if(i <= R) {
            len = Z[i - L + 1];
            if(i + len - 1 >= R) {
                for(L = i, R = i + len - 1, k = len; k <= LA && R <= LB && A[k] == B[R]; k++, R++);
                R--;
                len = R - L + 1;
            }
        }

        if(len == LA) {
            nrMatches++;
            if(nrMatches <= 1000)
                matches[nrMatches] = i - 1;
        }
    }
}

int main() {
    int i;

#ifndef ONLINE_JUDGE
    freopen(inputFile.c_str(), "r", stdin);
    freopen(outputFile.c_str(), "w", stdout);
#endif

    scanf("%s", A + 1);
    scanf("%s", B + 1);

    LA = strlen(A + 1);
    LB = strlen(B + 1);

    z_algo();

    matching();

    printf("%d\n", nrMatches);

    for(i = 1; i <= min(nrMatches, 1000); i++)
        printf("%d ", matches[i]);

    return 0;
}