Cod sursa(job #2145078)

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

vector<int>matches;

char a[nmax*2];
int Zarray[nmax],len1,len2,cnt;

void ZAlgorithm()
{
    int j=0;
    for (int i=1; i<len2; ++i)
    {
        int tlen=0,w;
        j=0;
        for (w=i; a[j+1]==a[w+1]; ++w,++j,++tlen);
        if (a[w]==a[j])
            Zarray[i]=tlen+1;
        else
            Zarray[i]=tlen;
    }
    for (int i=len1+1; i<len2; ++i)
        if (Zarray[i]==len1)
        {
            if (matches.size()<1000)
                matches.push_back(i-len1-1);
            else
                ++cnt;
        }
    g<<matches.size()+cnt<<'\n';
    for (auto w:matches)
        g<<w<<' ';
}

void read()
{
    f.getline(a,nmax);
    len1=strlen(a);
    a[len1]='$';
    f.getline(a+len1+1,nmax);
    len2=strlen(a);
}

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