Cod sursa(job #878142)

Utilizator dan.lincanDan Lincan dan.lincan Data 14 februarie 2013 02:18:55
Problema Potrivirea sirurilor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

void redirectInput(const char *in, const char *out)
{
    freopen(in, "r", stdin);
    //freopen(out, "w", stdout);
}

int hash(string &s, int start, int l)
{
    int x = 1;
    int base = 101;
    int hash = 0;
    for(int i = start; i < l; ++i)
    {
        hash += s[i] * x;
        x *= base;
    }
    return hash;
}

int hash(string &s, int start, int l, int prevHash)
{
    return prevHash - s[start - 1] + s[start + l];
}

void solve()
{
    char *a = (char*) calloc (2000000, sizeof(char));
    char *b = (char*) calloc (2000000, sizeof(char));
    scanf("%s %s", a, b);
    int n = strlen(a);
    int m = strlen(b);
    /*
    if( m < n )
        return;

    int hs = hash(a, 0, n);
    int h = hash(b, 0, n);

    if( h == hs && strncmp(a.c_str(), b.c_str(), n) == 0)
    {
        cout << 0 << endl;
    }

    vector<int> matches;

    for(int i = 1; i < m - n; ++i)
    {
        h = hash(b, i, n, h);
        if(h == hs)
        {
            if( strncmp(a.c_str(), b.c_str() + i, n) == 0)
                matches.push_back(i);
        }
    }
    int n_to_print = (matches.size() > 1000) ?  1000 : matches.size();
    for(int i = 0; i < n_to_print; ++i)
        cout << matches[i] << endl;
    */
}

int main(int argc, char *argv[])
{
    if(argc != 2)
        redirectInput("strmatch.in", "strmatch.out");
    else
        redirectInput(argv[1], "strmatch.out");

    solve();

    return 0;
}