Cod sursa(job #1376646)

Utilizator teoionescuIonescu Teodor teoionescu Data 5 martie 2015 18:10:55
Problema Potrivirea sirurilor Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include<fstream>
#include<cstring>
#include<vector>
using namespace std;
ifstream in("strmatch.in");
ofstream out("strmatch.out");
const int Nmax = 2000001;
char A[Nmax],B[Nmax];
int pi[Nmax],N,M;
vector<int> ans;
int main(){
    in.getline(A,Nmax); N=strlen(A);
    in.getline(B,Nmax); M=strlen(B);
    int w=0;
    for(int i=1;i<N;i++){
        while(w && A[w]!=A[i]) w=pi[w-1];
        if(A[w]==A[i]) w++;
        pi[i]=w;
    }
    w=0;
    for(int i=0;i<M;i++){
        while(w && A[w]!=B[i]) w=pi[w-1];
        if(A[w]==B[i]) w++;
        if(w==N) ans.push_back(i-N+1);
    }
    out<<ans.size()<<'\n';
    for(unsigned int i=0;i<ans.size();i++) out<<ans[i]<<' ';
    return 0;
}