HDU5558 Alice's Classified Message(合肥区域赛 后缀数组)
当初合肥区域赛的题(现场赛改了数据范围就暴力过了),可惜当初后缀数组算法的名字都没听过,现在重做下。
i从1到n - 1,每次枚举rank[i]附近的排名,并记录当起点小于i时的LCP(rank[i], d)的最大值,或是在LCP不变时更新起点。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
const int N = 110008, INF = 0x3F3F3F3F; int val[N], ws0[N], wa[N], wb[N];
int sa[N], rk[N], height[N]; char str[N]; bool cmp(int str[], int a, int b, int l){
return str[a] == str[b] && str[a + l] == str[b + l];
} void da(char str[], int n, int m){
int *x = wa, *y = wb; memset(ws0, 0, sizeof(ws0));
for(int i = 0; i < n; i++)
ws0[x[i] = str[i]]++; for(int i = 1; i < m; i++)
ws0[i] += ws0[i - 1]; for(int i = n - 1; i >= 0; i--)
sa[--ws0[ x[i]] ] = i; for(int j = 1, p = 1; p < n; j *= 2, m = p){
p = 0;
for(int i = n - j; i < n; i++)
y[p++] = i;
for(int i = 0; i < n; i++)
if(sa[i] >= j)
y[p++] = sa[i] - j;
for(int i = 0; i < n; i++)
val[i] = x[ y[i] ]; memset(ws0, 0, sizeof(ws0));
for(int i = 0; i < n; i++)
ws0[val[i]]++; for(int i = 1; i < m; i++)
ws0[i] += ws0[i - 1]; for(int i = n - 1; i >= 0; i--)
sa[--ws0[ val[i] ]] = y[i]; swap(x, y);
x[sa[0]] = 0;
p = 1;
for(int i = 1; i < n; i++)x[sa[i]] = cmp(y, sa[i - 1], sa[i], j)? p - 1:p++;
}
} void calHeight(char str[], int n){
for(int i = 1; i <= n; i++){
rk[ sa[i] ] = i;
}
int k = 0;
for(int i = 0; i < n; i++){
if(k){
k--;
}
int j = sa[rk[i] - 1];
while(str[i + k] == str[j + k]){
k++;
}
height[rk[i]] = k;
}
} int rmq[N][20], tab[N]; void initaskRMQ(int n){
tab[1] = 0;
for(int i = 2; i <= n; i++){
tab[i] = ( (i & (i - 1)) == 0)? tab[i - 1] + 1:tab[i - 1];
}
for(int i = 1; i <= n; i++){
rmq[i][0] = height[i];
}
for(int i = 1; i <= tab[n]; i++){
for(int j = 1; j <= n + 1 - (1 << i); j++){
int a = rmq[j][i - 1];
int b = rmq[j + (1 <<(i - 1))][i - 1];
rmq[j][i] = min(a, b);
}
}
} int askRMQ(int a, int b){
int k = tab[b -a + 1];
b -= (1 << k ) - 1;
return min(rmq[a][k], rmq[b][k]);
} int LCP(int a, int b){
if(a > b){
swap(a, b);
}
return askRMQ(a + 1, b);
} int main(){
int t;
cin>>t;
for(int cas = 1; cas <= t; cas++){
scanf("%s", str);
int n = strlen(str);
da(str, n + 1, 'z' + 1);
calHeight(str, n); initaskRMQ(n);
printf("Case #%d:\n", cas);
printf("%d %d\n", -1, str[0]);
for(int i = 1; i < n; ){
int st = INF, k = 1;
int d = rk[i] + 1;
int tp;
while(d <= n && (tp = LCP(d, rk[i])) >= k){
if(sa[d] < i){
if(tp > k){
k = tp;
st = sa[d];
}else{
if(sa[d] < st){
st = sa[d];
}
}
}
d++;
} d = rk[i] - 1;
while(d >= 1 && (tp = LCP(d, rk[i])) >= k){
if(sa[d] < i){
if(tp > k){
k = tp;
st = sa[d];
}else{
if(sa[d] < st){
st = sa[d];
}
}
}
d--;
}
if(st < i){
printf("%d %d\n", k, st);
i += k;
}else{
printf("%d %d\n", -1, (int)str[i]);
i++;
}
}
}
return 0;
}
加油吧! fighting!!!
HDU5558 Alice's Classified Message(合肥区域赛 后缀数组)的更多相关文章
- hdu5558 Alice's Classified Message
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=5558 题目: Alice's Classified Message Time Limit: 1 ...
- (HDU 5558) 2015ACM/ICPC亚洲区合肥站---Alice's Classified Message(后缀数组)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5558 Problem Description Alice wants to send a classi ...
- HDU5556 Land of Farms(二分图 2015 合肥区域赛)
容易想到将问题转化为求图的独立数问题 ,但求一般图的独立集是一个NPC问题,需要一些转化. 状态压缩,枚举每个上古农场是否选择,然后将剩下的新农场根据i + j奇偶性分为x , y集. 结果为 max ...
- HUID 5558 Alice's Classified Message 后缀数组+单调栈+二分
http://acm.hdu.edu.cn/showproblem.php?pid=5558 对于每个后缀suffix(i),想要在前面i - 1个suffix中找到一个pos,使得LCP最大.这样做 ...
- 【HDOJ5558】Alice's Classified Message(后缀数组)
题意:给定一个长度为n的下标从0开始的小写字母字符串,每次对于当前的i寻找一个j使得后缀i与后缀j的lcp最大,(j<i)若lcp相同则取较小的 若lcp为0则输出0与当前字符,i自增1,否则输 ...
- Alice's Classified Message HDU - 5558 后缀自动机求某个后缀出现的最早位置
题意: 给定一个长度不超过 10W 的只包含小写字母的字符串,从下标 0 到 n−1.从下标 0 开始操作, 每次对于下标 pos查找下标 pos 开始的子串中最长的在其他地方出现过的长度,其他出现的 ...
- HDU 5558 Alice's Classified Message(后缀数组+二分+rmq(+线段树?))
题意 大概就是给你一个串,对于每个\(i\),在\([1,i-1]\)中找到一个\(j\),使得\(lcp(i,j)\)最长,若有多个最大\(j\)选最小,求\(j\)和这个\(lcp\)长度 思路 ...
- POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀数组 倍增)
题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 比后缀自动机慢好多(废话→_→). \(Description\) 求两个字符串最长公共子串 ...
- HDU 6194 string string string 2017沈阳网络赛 后缀数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6194 题意:告诉你一个字符串和k , 求这个字符串中有多少不同的子串恰好出现了k 次. 解法:后缀数组 ...
随机推荐
- windows下的getopt/getoptlong函数
windows下的getopt/getoptlong函数 getopt/getopt_long函数是GNU C中的函数,在linux编程中很常用到.这里就不介绍了. windows下没有找到类似的函数 ...
- PyQt4软件打包成exe文件
使用py2exe进行打包 例: from distutils.core import setupimport py2exeimport sys sys.argv.append('py2exe') py ...
- ubuntu14.04 archive sources.list
deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse deb http://archive. ...
- Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- IPC----哲学家就餐问题(并发与互斥)
哲学家就餐问题描述: 5个哲学家,5个筷子.5个哲学家围坐在一张桌子上,筷子放在分别放在每个哲学家的两旁.如果所有哲学家在某个时刻同时拿起左边的筷子,那么右边的筷子就都被其他的哲学家拿了,造成大家都无 ...
- struts2 复杂参数封装
1.1.1 Struts2中封装复杂类型的数据: 封装到List集合: 页面: 商品名称:<input type="text" name="products[ ...
- Qt 信号槽如何传递参数(或带参数的信号槽)
信号槽如何传递参数(或带参数的信号槽) 利用Qt进行程序开发时,有时需要信号槽来完成参数传递.带参数的信号槽在使用时,有几点需要注意的地 ...
- python virtualenv环境运行django
python virtualenv环境运行django 安装前准备 检查pip版本与python版本是否一致 [root@localhost bin]# whereis pip pip: /usr/b ...
- /etc/securetty文件
/etc/securetty 文件允许你规定“root”用户可以从哪个tty设备登录. 登录程序(通常是/bin/login)需要读取“/etc/securetty”文件. 它的格式:列出来的tty设 ...
- 7. javacript高级程序设计- 函数表达式
1. 函数表达式 1.1 函数定义 函数定义的方式有两种:一种是函数声明,另一种就是函数表达式. (1). 函数声明:函数声明的重要特征就是函数声明提示,函数声明会在函数执行前执行 function ...