Cod sursa(job #3156373)

Utilizator alexdumitruAlexandru Dumitru alexdumitru Data 11 octombrie 2023 12:43:10
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("strmatch.in");
ofstream fout("strmatch.out");

const int NMAX = 4e6 + 5;

int n;
string a, b;
vector<int> pos;
int l, r, cnt;
int z[NMAX];

void solve()
{
    fin >> a >> b;
    a = "#" + a + "%" + b;
    n = a.size() - 1;
    for(int i = 2; i <= n; i++)
    {
        if(i <= r)
            z[i] = min(z[i - l + 1], r - i + 1);
        while(i + z[i] <= n && a[z[i] + 1] == a[i + z[i]])
            z[i]++;
        if(i + z[i] - 1 > r)
        {
            r = i + z[i] - 1;
            l = i;
        }
    }
    for(int i = n - b.length() + 1; i <= n; i++)
        if(z[i] == n - b.length() - 1)
        {
            cnt++;
            if(cnt <= 1000)
                pos.push_back(i - n + b.length() - 1);
        }
    fout << cnt << '\n';
    for(int x : pos)
        fout << x << ' ';
    fout << '\n';
}

int main()
{
    solve();
    return 0;
}