Cod sursa(job #3286577)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 14 martie 2025 13:29:39
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream cin ("strmatch.in");
ofstream cout ("strmatch.out");

const int N = 5e6 + 1;
int z[N + 1];

string a, b;

vector <int> sol;

int main()
{
    cin >> a >> b;
    int m = a.size();
    a = a + '@' + b;
    for (int i = 1, l = 0, r = 0; i < a.size(); ++i)
    {
        if (i <= r)
            z[i] = min (z[i - l], r - i + 1);
        while (i + z[i] < a.size() && a[z[i]] == a[i + z[i]])
            ++z[i];
        if (i + z[i] - 1 > r)
            r = i + z[i] - 1, l = i;
    }
    for (int i = m + 1; i < a.size(); ++i)
        if (z[i] == m)
        {
            if (sol.size() <= 999)
                sol.push_back(i - m - 1);
        }
    cout << sol.size() << '\n';
    for (auto it : sol)
        cout << it << ' ';
    return 0;
}