Cod sursa(job #1967198)

Utilizator FckingSlayerSlayer99 FckingSlayer Data 16 aprilie 2017 01:51:11
Problema Substr Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <iostream>
#include <fstream>
#include <cstdio>
#include <queue>
#include <vector>
#include <string.h>
#include <algorithm>
#include <limits.h>
#include <iomanip>
#include <cmath>

#define nMax 17390
#define log2Max 19
#define eulMax 64004
#define pb push_back
#define mkp make_pair
#define x first
#define y second

using namespace std;

ifstream fin("substr.in");
ofstream fout("substr.out");

int n, k, stp, cnt;
int P[log2Max][nMax], which[nMax];
pair<pair<int, int>, int> L[nMax];
char sir[nMax];

int query(int f, int s)
{
    int rez=0;
    if(f==s)
        return n-f+1;
    for(int k=stp; k>=0 && f<=n && s<=n; k--)
    {
        if(P[k][f]==P[k][s])
        {
            rez+=(1 << k);
            f+=(1 << k);
            s+=(1 << k);
        }
    }
    return rez;
}

int main()
{
    fin>>n>>k;
    fin>>sir+1;

    for(int i=1; i<=n; i++)
        P[0][i]=sir[i]-'a';
    for(stp=1, cnt=1; (cnt >> 1)<=n; stp++, cnt <<= 1)
    {
        for(int i=1; i<=n; i++)
        {
            L[i].x.x=P[stp-1][i];
            L[i].x.y=(i+cnt<=n ? P[stp-1][i+cnt] : -1);
            L[i].y=i;
        }
        sort(L+1, L+n+1);

        for(int i=1; i<=n; i++)
            P[stp][L[i].y]=(i>1 && L[i].x.x==L[i-1].x.x && L[i].x.y==L[i-1].x.y ? P[stp][L[i-1].y] : i);
    }
    stp--;
    for(int i=1; i<=n; i++)
        which[P[stp][i]]=i;
    int Max=0;
    for(int i=1; i+k-1<=n; i++)
        Max=max(Max, query(which[i], which[i+k-1]));
    fout<<Max;
}