Cod sursa(job #1879624)

Utilizator sfechisalin@yahoo.comSfechis Alin [email protected] Data 15 februarie 2017 01:45:30
Problema Potrivirea sirurilor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");

string A,B,S;

int main()
{
	getline(fin,S);
	B = " ";
	B += S;
	getline(fin,S);
	A = " ";
	A += S;
     int k = 0 , m = B.size();
    vector<int>answ;
    vector<int>pi(m,0);
    for (int i = 2; i <= m; ++i)
    {
        while (k > 0 && B[k+1] != B[i])
            k = pi[k];
        if (B[k+1] == B[i])
            k++;
        pi[i] = k;
    }

    for(int i = 1; i <= A.size(); ++i)
    {
        while (k > 0 && B[k+1] != A[i])
            k = pi[k];
        if (B[k+1] == A[i])
            k++;
        if (k == m - 1)
        {
            answ.push_back(i-m+1);
           k = pi[k];
        }
    }
    int d = answ.size();
    int length = min(d,1000);
    fout << length << "\n";
    for (int i = 0; i < length; ++i)
        fout << answ[i] << " ";
    return 0;
}