Cod sursa(job #3225375)

Utilizator Alex_DumitrascuAlex Dumitrascu Alex_Dumitrascu Data 17 aprilie 2024 14:36:06
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("strmatch.in");
ofstream fout ("strmatch.out");
int lps[2000005];
vector <int> ap;
int main()
{
    fin.tie(0); fin.sync_with_stdio(false);
    string pre; fin>>pre;
    string s; fin>>s;
    int match=0, index=1;
    lps[0]=0;
    while (index<(int)pre.size()) {
        //cout<<pre[index]<<' '<<pre[match]<<endl;
        if (pre[index]==pre[match]) {
            match++;
            lps[index]=match;;
            index++;
        }
        else {
            if (match==0) index++;
            else match=lps[match-1];
        }
    }
    for (int i=0; i<(int)pre.size(); i++) cout<<lps[i]<<' ';
    index=0; match=0;
    while (index<(int)s.size()) {
        if (s[index]==pre[match]) {
            index++;
            match++;
            if (match==(int)pre.size()) {
                ap.push_back(index-match);
                match=lps[match-1];
            }
        }
        else {
            if (match==0) index++;
            else match=lps[match-1];
        }
    }
    fout<<ap.size()<<'\n';
    for (int i=0; i<min((int)ap.size(), 1000); i++) {
        fout<<ap[i]<<' ';
    }
    return 0;
}