Cod sursa(job #2574425)

Utilizator Fatu_SamuelFatu Samuel Fatu_Samuel Data 5 martie 2020 22:11:06
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 2000005;

string p, t;

int lps[nmax];

void CreateLPS()
{
    int len = 0, k = 1, s = p.length();

    while (k < s)
    {
        if (p[k] == p[len])
            lps[k++] = ++len;
        else
        {
            if (len)
                len = lps[len - 1];
            else
                k++;
        }
    }
}


int main()
{
    fin >> p >> t;

    fout << "                                            \n";

    CreateLPS();

    int i = 0, j = 0, s = t.length(), c = 0;


    while (i < s)
    {
        if (p[j] == t[i])
        {
            i++;
            j++;
        }
        else
        {
            if (j)
                j = lps[j - 1];
            else
                i++;
        }

        if (j == p.length())
        {
            if (c < 1000)
                fout << i - j << ' ';

            c++;
        }
    }

    fout.close();
    fout.open("strmatch.out", ios::in|ios::out);

    fout << c;

    fin.close();
    fout.close();
    return 0;
}