Cod sursa(job #1412335)

Utilizator mucenic_b101Bogdan Mucenic mucenic_b101 Data 1 aprilie 2015 11:28:03
Problema Substr Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <fstream>
#include <iostream>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <deque>
#define MAXN 16384
#define LOG 14
#define P 73
#define MOD1 100007
#define MOD2 100021
using namespace std;

class HshClass {
    public: inline size_t operator()(const pair<int, int> &a) const {
        return a.first + a.second;
    }
};

typedef pair <int, int> hashu;
typedef unordered_map <hashu, int, HshClass> mapa;
string s;
mapa M;

int main () {
    ifstream cin("substr.in");
    ofstream cout("substr.out");

    int n, k;
    cin >> n >> k >> s;
    //cerr<<s<<"\n";

    int sol = 0, pas = (1 << LOG);
    while (pas) {
        int len = sol + pas;
        if (len <= n) {
            M.clear();
            int hash1, hash2, P1, P2;
            hash1 = hash2 = 0;
            P1 = P2 = 1;

            for (int i = 0 ; i < len ; ++i) {
                hash1 = ((hash1 * P) + s[i]) % MOD1;
                hash2 = ((hash2 * P) + s[i]) % MOD2;

                if (i != 0) {
                    P1 = (P1 * P) % MOD1;
                    P2 = (P2 * P) % MOD2;
                }
            }

            bool ok = false;
            M[make_pair(hash1, hash2)]++;
            if (k == 1)
                ok = true;

            for (int i = len ; i < n ; ++i) {
                hash1 = (((hash1 - (s[i - len] * P1) % MOD1) + MOD1) * P + s[i]) % MOD1;
                hash2 = ((hash2 - ((s[i - len] * P2) % MOD2) + MOD2) * P + s[i]) % MOD2;

                M[make_pair(hash1, hash2)]++;
                if (M[make_pair(hash1, hash2)] >= k)
                    ok = true;
            }

            if (ok)
                sol = len;
        }

        pas >>= 1;
    }

    cout << sol << "\n";
    return 0;
}