Cod sursa(job #1769711)

Utilizator sulzandreiandrei sulzandrei Data 2 octombrie 2016 23:32:26
Problema Potrivirea sirurilor Scor 32
Compilator cpp Status done
Runda Arhiva educationala Marime 1.56 kb
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <limits>
using namespace std;

ifstream in("strmatch.in");
ofstream out("strmatch.out");
#define nr_char 256
bool PksuffixPqa(const string & P,int k,int q,int a)
{
    if(k == -1)
        return true;
    if(P[k]!=a)         //
        return false;
    for(int i = k-1 ; i >= 0 ; i--,q--)
        if(P[i]!=P[q])
            return false;
    return true;
}
int** precomputeDeltaFunction(const string& P)
{

    bool ok;
    int m = P.size(),k;
    int** delta = new int*[m+1];
    for(int i = 0 ; i <= m ; i++)
        delta[i] = new int[nr_char]{};

    for(int q = 0 ;  q <= m ; q++)
        for(int a = 0 ; a < nr_char; a++)//sigma
        {
            k = min(m+1,q+2);
            do
            {
                k--;
                ok = PksuffixPqa(P,k-1,q-1,a);
            }
            while(!ok);
            delta[q][a] = k;
        }
    return delta;
}
void finiteStateAutomaton(const string& T,int** delta,int m,vector<int>& pos)
{
    int n = T.size();
    if(m>n)
        return;
    int q = 0;
    for(int i = 0 ; i < n ; i++)
    {
        q = delta[q][(int)T[i]];
        if(q == m)
            pos.push_back(i-m+1);
    }
}
int main()
{
    vector<int> pos;
    string s,w;
    in >> w >> s;

    finiteStateAutomaton(s,precomputeDeltaFunction(w),w.size(),pos);

    out<<pos.size()<<'\n';
    for(int j = 0 ; j < std::min((int)pos.size(),1000); j++)
        out<<pos[j]<<" ";

    return 0;
}