• POJ3261:Milk Patterns


    题面

    vjudge

    Sol

    二分答案+分组,判断有没有一个组的后缀个数不小于 k
    做法

    # include <bits/stdc++.h>
    # define IL inline
    # define RG register
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(20010);
    
    IL ll Read(){
    	RG char c = getchar(); RG ll x = 0, z = 1;
    	for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    	for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    	return x * z;
    }
    
    int n, a[_], sa[_], rk[_], y[_], h[_], height[_], t[1000010], ans, K, mx;
    
    IL bool Cmp(RG int i, RG int j, RG int k){  return y[i] == y[j] && y[i + k] == y[j + k];  }
    
    IL void Sort(){
    	RG int m = mx;
    	for(RG int i = 1; i <= n; ++i) ++t[rk[i] = a[i]];
    	for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
    	for(RG int i = n; i; --i) sa[t[rk[i]]--] = i;
    	for(RG int k = 1; k <= n; k <<= 1){
    		RG int l = 0;
    		for(RG int i = n - k + 1; i <= n; ++i) y[++l] = i;
    		for(RG int i = 1; i <= n; ++i) if(sa[i] > k) y[++l] = sa[i] - k;
    		for(RG int i = 0; i <= m; ++i) t[i] = 0;
    		for(RG int i = 1; i <= n; ++i) ++t[rk[y[i]]];
    		for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
    		for(RG int i = n; i; --i) sa[t[rk[y[i]]]--] = y[i];
    		swap(rk, y); rk[sa[1]] = l = 1;
    		for(RG int i = 2; i <= n; ++i) rk[sa[i]] = Cmp(sa[i - 1], sa[i], k) ? l : ++l;
    		if(l >= n) break; m = l;
    	}
    	for(RG int i = 1; i <= n; ++i){
    		h[i] = max(0, h[i - 1] - 1);
    		if(rk[i] == 1) continue;
    		while(a[i + h[i]] == a[sa[rk[i] - 1] + h[i]]) ++h[i];
    	}
    	for(RG int i = 1; i <= n; ++i) height[i] = h[sa[i]];
    }
    
    IL bool Check(RG int x){
    	RG int cnt = 0;
    	for(RG int i = 2; i <= n; ++i){
    		if(height[i] < x) cnt = 0;
    		else cnt++;
    		if(cnt >= K - 1) return 1;
    	}
    	return 0;
    }
    
    int main(RG int argc, RG char* argv[]){
    	n = Read(); K = Read();
    	for(RG int i = 1; i <= n; ++i) a[i] = Read(), mx = max(a[i], mx);
    	Sort();
    	RG int l = 0, r = n;
    	while(l <= r){
    		RG int mid = (l + r) >> 1;
    		if(Check(mid)) ans = mid, l = mid + 1;
    		else r = mid - 1;
    	}
    	printf("%d
    ", ans);
    	return 0;
    }
    
    
  • 相关阅读:
    react Native 运行报错之一 gradle-2.14.1-all解压失败的问题
    react native windows create bundle folder
    gulp+browserSync+nodemon 实现express 全端自动刷新的实践
    nodejs框架express4.x 学习--安装篇
    转: angularjs 指令中动态编译的方法(适用于有异步请求的情况) 内嵌指令无效
    angular 基础练习
    自己写的数组排重+排序
    前端开发bower包管理器
    定位网站性能的一些经验
    记一次大规模数据迁移和加密
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8337742.html
Copyright © 2020-2023  润新知