Cod sursa(job #1636175)

Utilizator daniel.grosuDaniel Grosu daniel.grosu Data 6 martie 2016 23:21:41
Problema Potrivirea sirurilor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.58 kb
// Template v2
#define pb push_back
#define mp make_pair
#define first x
#define second y
#define l(x) x<<1
#define r(x) x<<1 | 1
#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PKK;
// primes less than 100
const int PRIM[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
const int CMAX = 10005;
const int MOD = 1000000007;
const int NMAX = 50000;
const short INF16 = 32000;
const int INF = int(1e9);
const LL INF64 = LL(1e18);
const LD EPS = 1e-9, PI = acos(-1.0);

int n,m;

int pre[2000005];

string s,t;
int k, rs[1005];

void prekmp()
{
    int q=0;
    pre[0]=0;
    for(int i=1; i<s.size(); ++i)
    {
        while(q && s[q] != s[i])
            q=pre[q];
        if(s[q] == s[i])
            q++;
        pre[i]=q;
    }
}

void read()
{
    cin>>s;
    cin>>t;
    prekmp();
    int q=0;
    for(int i=0; i<t.size(); ++i)
    {
        while(q && s[q] != t[i])
            q=pre[q-1];
        if(s[q] == t[i]){
            q++;
        }
        if(q == s.size())
        {
            k++;
            q=pre[q-1];
            if(k<=1000)
                rs[k]=i-s.size()+1;
        }
    }
    cout<<k<<"\n";
    for(int i=1; i<=min(k, 1000); ++i)
        cout<<rs[i]<<" ";
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    cout << setprecision(16) << fixed;
    
    assert(freopen("kmp.in", "rt", stdin));
    assert(freopen("kmp.out", "wt", stdout));
    
    read();

    return 0;
}