Cod sursa(job #1289959)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 10 decembrie 2014 17:15:33
Problema Potrivirea sirurilor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.76 kb
#include<cstdio>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<bitset>
#include<deque>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<unordered_map>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

const int nmax = 2000005;
const int mod1 = 666013;
const int mod2 = 100003;
const int base = 137;

int n, m, i, code1, code2, match1, match2, b1, b2, cnt, sol[nmax];
char a[nmax], b[nmax];

int main()
{
    freopen("strmatch.in", "r", stdin);
    freopen("strmatch.out", "w", stdout);

    scanf("%s", a + 1);
    scanf("%s", b + 1);

    n = strlen(a + 1);
    m = strlen(b + 1);

    b1 = b2 = 1;
    for(i = 1; i <= n; i++)
    {
        code1 = (code1 * base + a[i]) % mod1;
        code2 = (code2 * base + a[i]) % mod2;

        if(i < n)
        {
            b1 = (b1 * base) % mod1;
            b2 = (b2 * base) % mod2;
        }
    }

    for(i = 1; i <= n; i++)
    {
        match1 = (match1 * base + b[i]) % mod1;
        match2 = (match2 * base + b[i]) % mod2;
    }

    if(code1 == match1 && code2 == match2)
        sol[++cnt] = 0;

    for(i = n + 1; i <= m; i++)
    {
        match1 = ((match1 - (b1 * b[i - n]) % mod1 + mod1) * base + b[i]) % mod1;
        match2 = ((match2 - (b2 * b[i - n]) % mod2 + mod2) * base + b[i]) % mod2;

        if(code1 == match1 && code2 == match2)
        {
            if(++cnt <= 1000)
                sol[cnt] = i - n;
        }
    }

    printf("%d\n", cnt);
    for(i = 1; i <= min(1000, cnt); i++)
        printf("%d ", sol[i]);

    return 0;
}