POJ 3261 (后缀数组 二分) Milk Patterns
这道题和UVa 12206一样,求至少重复出现k次的最长字串。
首先还是二分最长字串的长度len,然后以len为边界对height数组分段,如果有一段包含超过k个后缀则符合要求。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
const int maxm = + ; int s[maxn];
int sa[maxn], height[maxn], rank[maxn];
int t[maxn], t2[maxn], c[maxm];
int n, k; void build_sa(int m)
{
int i, *x = t, *y = t2;
for(i = ; i < m; i++) c[i] = ;
for(i = ; i < n; i++) c[x[i] = s[i]]++;
for(i = ; i < m; i++) c[i] += c[i - ];
for(i = n - ; i >= ; i--) sa[--c[x[i]]] = i;
for(int k = ; k <= n; k <<= )
{
int p = ;
for(i = n - k; i < n; i++) y[p++] = i;
for(i = ; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
for(i = ; i < m; i++) c[i] = ;
for(i = ; i < n; i++) c[x[y[i]]]++;
for(i = ; i < m; i++) c[i] += c[i - ];
for(i = n - ; i >= ; i--) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = ; x[sa[]] = ;
for(i = ; i < n; i++)
x[sa[i]] = y[sa[i]]==y[sa[i-]] && y[sa[i]+k]==y[sa[i-]+k] ? p - : p++;
if(p >= n) break;
m = p;
}
} void build_height()
{
int i, j, k = ;
for(i = ; i < n; i++) rank[sa[i]] = i;
for(i = ; i < n; i++)
{
if(k) k--;
j = sa[rank[i] - ];
while(s[i + k] == s[j + k]) k++;
height[rank[i]] = k;
}
} bool ok(int len)
{
int cnt = ;
for(int i = ; i < n; i++)
{
if(i == || height[i] < len) cnt = ;
if(++cnt >= k) return true;
}
return false;
} int main()
{
//freopen("in.txt", "r", stdin); scanf("%d%d", &n, &k);
for(int i = ; i < n; i++)
{
scanf("%d", &s[i]);
s[i]++;
}
s[n] = ;
build_sa();
build_height(); int L = , R = n;
while(L < R)
{
int M = (L + R + ) / ;
if(ok(M)) L = M;
else R = M - ;
} printf("%d\n", L); return ;
}
代码君
POJ 3261 (后缀数组 二分) Milk Patterns的更多相关文章
- POJ 3261 后缀数组+二分
思路: 论文题- 二分+对后缀分组 这块一开始不用基数排序 会更快的(其实区别不大) //By SiriusRen #include <cstdio> #include <cstri ...
- Milk Patterns POJ - 3261 后缀数组
Farmer John has noticed that the quality of milk given by his cows varies from day to day. On furthe ...
- POJ 3261 后缀数组
题目链接:http://poj.org/problem?id=3261 题意:约翰注意到奶牛产奶的之类是不断变化的,虽然他不能预测从当天到下一天的变化情况但是他知道变化是有规律的,牛奶的质量由一个整数 ...
- poj 3261 后缀数组 找反复出现k次的子串(子串能够重叠)
题目:http://poj.org/problem?id=3261 仍然是后缀数组的典型应用----后缀数组+lcp+二分 做的蛮顺的,1A 可是大部分时间是在调试代码.由于模板的全局变量用混了,而自 ...
- poj 3261 后缀数组 可重叠的 k 次最长重复子串
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 16430 Accepted: 7252 Ca ...
- POJ 2774 后缀数组 || 二分+哈希
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 35607 Accepted: 14 ...
- POJ 1743 (后缀数组 二分) Musical Theme
看来对height数组进行分段确实是个比较常用的技巧. 题意: 一个主题是可以变调的,也就是如果这个主题所有数字加上或者减少相同的数值,可以看做是相同的主题. 一个主题在原串中至少要出现两次,而且一定 ...
- [poj 3261]后缀数组+滑窗最小值
题目链接:http://poj.org/problem?id=3261 这个是可以交叉的重复串,所以用height就可以了,但是题目说让重复k次以上,也就是直接做一个k-1长度的滑窗最小值,从这些最小 ...
- POJ - 3261 后缀数组 height应用
题意:求最少重叠\(k\)次的重复子串的最大长度 子串长度问题依然是二分枚举,可以观察出重叠的一定是sa排序中连续的 之前想出一种判断要\(n^2\)的方法,没有考虑到后面肯定会连续出现的情况 (大概 ...
随机推荐
- ASP.NET用户控件事件的定义和实践
假定用户控件(UserControl.ascx)中包含按钮控件 AButton,希望实现按 Button 按钮时,包含该用户控件的页面可以接收到事件. UserControl.ascx.cs ...
- mysql 函数,关键字,特性
## mysql 截取函数 left(),right(),substring(),substring_index()SELECT LEFT('www.baidu.com',3); # wwwSELEC ...
- Sqli-labs less 24
Less-24 Ps:本关可能会有朋友和我遇到一样的问题,登录成功以后没有修改密码的相关操作.此时造成问题的主要原因是logged-in.php文件不正确.可重新下载解压,解压过程中要主要要覆盖. 本 ...
- Sqli-labs less 29
Less-29 首先先看下tomcat中的index.jsp文件 在apache的index.php中,sql语句为 $sql="SELECT * FROM users WHERE id=' ...
- 几个经常用到的字符串的截取(java)
几个经常用到的字符串的截取 string str="123abc456";int i=3;1 取字符串的前i个字符 str=str.Substring(0,i); // or ...
- loadrunner之Paramater在负载测试中的数据生成规则
前段时间在做性能测试的时候,基于业务的需求,使用到了Unique Number的参数类型. 脚本的业务是注册以alien开头,后面接数字的用户帐号,填写相关帐号信息.提交企业信息进行审核. 其中用户帐 ...
- VS2010中打开VS2013/VS2012项目
VS2010中打开VS2013/VS2012项目 (2014-04-03 23:47:53) 转载▼ 分类: IT VS低版本打开高版本创建的项目时会提示"选择的文件是解决方案文件,但是 ...
- http://linuxme.blog.51cto.com/1850814/372960
http://linuxme.blog.51cto.com/1850814/372960 http://os.51cto.com/art/201404/435129.htm
- 数组使用find查询用法
#include "stdafx.h"#include <string>#include <list>#include <algorithm># ...
- iOS 开发压缩--SSZipArchive
// 解压 NSString *zipPath = @"被解压的文件路径"; NSString *destinationPath = @"解压到的目录"; [S ...