Cod sursa(job #2276051)

Utilizator Vlad_lsc2008Lungu Vlad Vlad_lsc2008 Data 4 noiembrie 2018 00:12:22
Problema Potrivirea sirurilor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.75 kb
#include <iostream>
#include <cstdio>
#include <cstring>
#define nmax 2000010
using namespace std;
char a[nmax],b[nmax];
int pref[nmax];
int nrsol,sol[1010];

inline void prefix(char *s,int *p)
{
    int n=strlen(s+1);
    int k=0;
    p[1]=0;
    for(int i=2;i<=n;i++)
    {
        cout<<i<<' '<<k<<' '<<s[i]<<' '<<s[k+1]<<' ';
        while(k>0 && s[i]!=s[k+1])
           {
               cout<<"\nsafa "<<i<<' '<<s[i]<<' '<<s[k+1]<<' '<<k<<' '<<p[k]<<' ';
                k=p[k];
                cout<<k<<'\n';
           }
        if(i==4) cout<<'\n'<<i<<' '<<k<<' '<<s[i]<<' '<<s[k+1]<<'\n';
        if(s[i]==s[k+1])
            k++;
        if(i==4) cout<<'\n'<<k<<'\n';
        p[i]=k;
        cout<<i<<' '<<k<<' '<<'\n';
        for(int j=1;j<=n;j++) cout<<p[j]<<' '; cout<<'\n';
    }
}

inline void potr(char *s,char *s1,int *p)
{
    int i;
    int m=strlen(s1+1);
    int n=strlen(s+1);
    int k=0;
    p[1]=0;
    for(i=1;i<=m;i++)
    {
        //cout<<i<<' '<<k<<" SDADF ";
        while(k>0 && s[k+1]!=s1[i])
            k=p[k];
        if(s[k+1]==s1[i])
            k++;
        if(k==n)
        {
            if(nrsol<1000)
            {
                nrsol++;
                sol[nrsol]=i-n+1;
            }
            else nrsol++;
        }
        //cout<<i<<' '<<k<<'\n';
    }
}

int main()
{
    freopen("strmatch.in","r",stdin);
    freopen("strmatch.out","w",stdout);
    gets(a+1);
    gets(b+1);
    prefix(a,pref);
  //  for(int i=1;i<=25;i++) cout<<pref[i]<<' '; cout<<'\n';
    potr(a,b,pref);
    printf("%d\n",nrsol);
    for(int i=1;i<=nrsol;i++)
    {
        if(i>1000) break;
        printf("%d ",sol[i]-1);
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}