Cod sursa(job #1966864)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 15 aprilie 2017 16:59:01
Problema Potrivirea sirurilor Scor 28
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fi("strmatch.in");
ofstream fo("strmatch.out");

const int NMAX = 1 << 21;

vector<int> aps;
string a, b, c1, c2;
int n, m, q, apps;

int z[NMAX];

void make_z(const string &s, int z[]) {
    int n = s.size();
    for (int i = 1; i < n; ++i) {
        while (i + z[i] < n && s[z[i]] == s[i + z[i]])
            ++z[i];
            z[i + z[i]] = max(z[i + z[i]], min(n - i - z[i], z[z[i]])); } }

int main() {
    string str, pat;

    fi >> pat >> str;
    str = pat + "#" + str;

    make_z(str, z);

    cout << str << '\n';
    for (int i = 0; i < str.size(); ++i)
        cout << z[i] << " \n"[i == str.size()];

    for (int i = 1; i < str.size(); ++i)
        if (z[i] == pat.size()) {
            ++apps;
            if (apps <= 1000)
                aps.push_back(i - pat.size() - 1); }

    fo << apps << '\n';
    for (auto i: aps)
        fo << i << ' ';
    fo << '\n';

    return 0; }