Cod sursa(job #2844841)

Utilizator andu2006Alexandru Gheorghies andu2006 Data 5 februarie 2022 17:58:40
Problema Potrivirea sirurilor Scor 38
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD1=1e9+7,MOD2=1e9+9,NMAX=2e6+5;
int p[NMAX*2];
char s[NMAX];
bool ans[NMAX*2];
int main()
{
    ifstream fin("strmatch.in");
    ofstream fout("strmatch.out");
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n=0,m=0;
    char c;
    while(fin.get(c)){
        if(c==EOF) break;
        if(c=='\n' && !m) c='#',m=n;
        s[n++]=c;
    }
    if(s[n-1]=='\n')
        s[--n]='\0',n--;
    //fout<<s;
    for(int i=1;i<n;i++){
        int j=p[i-1];
        while(j && s[i]!=s[j])
            j=p[j-1];
        if(s[i]==s[j]) j++;
        p[i]=j;
    }
    int cnt=0;
    for(int i=2*m;i<n;i++)
        if(p[i]==m)
            ans[i-2*m]=1,++cnt;
    fout<<cnt<<'\n';
    for(int i=0;i<n-2*m;i++)
        if(ans[i])
            fout<<i<<' ';
    return 0;
}