Cod sursa(job #2144455)

Utilizator NannyiMaslinca Alecsandru Mihai Nannyi Data 26 februarie 2018 19:16:24
Problema Potrivirea sirurilor Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <iostream>
#include <cstring>
#include <fstream>
#include <vector>
using namespace std;
#define nmax 2000005
ifstream f("strmatch.in");
ofstream g("strmatch.out");

vector<int>stk;

char a[nmax],b[nmax];
int preKMP[nmax];
int len1,len2;

void read()
{
    f.getline(a,nmax);
    f.getline(b,nmax);
    len1=strlen(a);
    len2=strlen(b);
}

void prekmp()
{
    int j=0;
    for (int i=1; i<len1; ++i)
    {
        while (j>0&&a[j]!=a[i])
        {
            j=preKMP[j];
        }
        if (a[j]==a[i])
            ++j;
        preKMP[i]=j;
    }
}

void kmp()
{
    int j=0;
    for (int i=0; i<len2; ++i)
    {
        while (j>0&&a[j]!=b[i])
        {
            j=preKMP[j];
        }
        if (b[i]==a[j])
            ++j;
        if (j==len1)
        {
            if (stk.size()==1000)
                break;
            stk.push_back(i-len1+1);
            --j;
        }
    }
    g<<stk.size()<<'\n';
    for (auto w:stk)
        g<<w<<' ';
}

void solve()
{
    prekmp();
    kmp();
}

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