Cod sursa(job #1052542)

Utilizator cdascaluDascalu Cristian cdascalu Data 11 decembrie 2013 15:14:48
Problema Substr Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <cstdio>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <unordered_map>

#define Nmax 17000
using namespace std;

char buf[Nmax];
int N,K,sol = -1, sol_max =-1;
int run(int sz)
{
	unordered_map<string, int> hash;
	string x = "";
    for(int i=0;i<sz;++i)
        x+= buf[i];
    hash[x] = 1;
    int maxim=-1;
     
    for(int i=sz;i<N;++i)
    {
        x += buf[i];
        x = x.substr(1, x.size() - 1);
     
        if(hash.find(x) != hash.end())
            hash[x] = hash[x] + 1;
        else
            hash[x] = 1;
        if(hash[x] > maxim)
            maxim = hash[x];
    }
    return maxim;
}
int main()
{
	ifstream f("substr.in");
	f>>N>>K;
	f.getline(buf,Nmax);
	f.getline(buf,Nmax);
	
	f.close();
	
	
	int left,right,mid;
	left = 0;right = N-1;
	
	while(left<=right)
	{
		mid = (left+right)/2;
		int x = run(mid);
		
		if(x >= K)
		{
			if(mid > sol)
				sol = mid;
			left = mid+1;
		}
		else
		{
			right = mid-1;
		}
	}
	ofstream g("substr.out");
	g<<sol<<"\n";
	g.close();
	return 0;
}