UVALive 6869(后缀数组)
题意:给定一个字符串,求至少重复一次的不同子串个数。
分析:模拟写出子符串后缀并排好序可以发现,每次出现新的重复子串个数都是由现在的height值减去前一个height值。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = ;
typedef long long LL;
int sa[maxn];
int t1[maxn], t2[maxn], c[maxn];
int ran[maxn], height[maxn];
int s[maxn];
char str[maxn]; void build_sa(int s[], int n, int m) {
int i, j, p, *x = t1, *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 (j = ; j <= n; j <<= ) {
p = ;
for (i = n-j; i < n; i++) y[p++] = i;
for (i = ; i < n; i++)
if (sa[i] >= j)
y[p++] = sa[i] - j;
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-]+j] == y[sa[i]+j] ? p- : p++; if (p >= n) break;
m = p;
}
} void getHeight(int s[],int n) {
int i, j, k = ;
for (i = ; i <= n; i++)
ran[sa[i]] = i;
for (i = ; i < n; i++) {
if (k) k--;
j = sa[ran[i]-];
while (s[i+k] == s[j+k]) k++;
height[ran[i]]=k;
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%s", str);
int n = strlen(str);
for (int i = ; i <= n; i++)
s[i] = str[i];
build_sa(s, n+, );
getHeight(s, n);
LL ans = ;
for (int i = ; i <= n; i++)
{
if(height[i]>height[i-])ans+=height[i]-height[i-];
}
printf("%lld\n", ans);
}
return ;
}
UVALive 6869(后缀数组)的更多相关文章
- UVALive - 6869 Repeated Substrings 后缀数组
题目链接: http://acm.hust.edu.cn/vjudge/problem/113725 Repeated Substrings Time Limit: 3000MS 样例 sample ...
- UVALive - 4513 Stammering Aliens ——(hash+二分 || 后缀数组加二分)
题意:找一个出现了m次的最长子串,以及这时的最右的位置. hash的话代码还是比较好写的,,但是时间比SA多很多.. #include <stdio.h> #include <alg ...
- UVALive - 6856 Circle of digits 后缀数组+二分
题目链接: http://acm.hust.edu.cn/vjudge/problem/82135 Circle of digits Time Limit: 3000MS 题意 把循环串分割成k块,让 ...
- 后缀数组的倍增算法(Prefix Doubling)
后缀数组的倍增算法(Prefix Doubling) 文本内容除特殊注明外,均在知识共享署名-非商业性使用-相同方式共享 3.0协议下提供,附加条款亦可能应用. 最近在自学习BWT算法(Burrows ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- POJ1743 Musical Theme [后缀数组]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- 后缀数组(suffix array)详解
写在前面 在字符串处理当中,后缀树和后缀数组都是非常有力的工具. 其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料. 其实后缀数组是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现, ...
- 【UOJ #35】后缀排序 后缀数组模板
http://uoj.ac/problem/35 以前做后缀数组的题直接粘模板...现在重新写一下模板 注意用来基数排序的数组一定要开到N. #include<cstdio> #inclu ...
随机推荐
- SQL多个表实现联合查询
select LineId,Id,Country from Domestic union allselect LineId,Id,Country from Freedom-- 联合查询Domestic ...
- 130825组队赛-Regionals 2012, North America - East Central NA
A.Babs' Box Boutique 一道简单的dfs搜索题,需要两两比较,然后搜到底,得到最大值就行了.比赛时队友写的,我只负责debug..赛后自己写的.. #include<iostr ...
- 有关android工程的构建脚本(build.xml)的学习
学习[android-sdk-linux根目录]/tools/ant/build.xml,觉得如下几点很有用,记录之 1)ant脚本中属性值是于前置定义优化的原则,即属性发生重复定义时,前面定义的值不 ...
- Ch02 从零开始实例学习5
演练:添加模型 原文链接:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model ...
- 最简单的基于FFMPEG+SDL的视频播放器 ver2 (採用SDL2.0)
===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG ...
- Linux设备驱动中的ioctl
memdev.h #ifndef _MEMDEV_H #define _MEMDEV_H #define MEM_MAGIC 'm' #define MEM_RESTART _IO(MEM_MAGIC ...
- eclipse导入myeclipse的web项目没法识别问题解决方法
1.进入项目目录,找到.project文件,打开. 2.找到<natures>...</natures>代码段. 3.在第2步的代码段中加入如下标签内容并保存: <nat ...
- EasyUI - 后台管理系统 - 登陆模块
效果: --- --- Html代码: <div id="login"> <p>账户:<input type="text" id= ...
- Ubuntu Crontab
Ubuntu启用Crontab 启动cron服务: service cron start 如果需要设置为开机时自动启动,则执行 sysv-rc-conf --level 35 cron on 另外,u ...
- 熬之滴水成石:最想深入了解的内容--windows内核机制(15)
66--内存管理(4) 说说在windows中内存空间初始化的事,开始的开始通过处理器的分页机制,预先建立相应足够的页表以便页表来访问物理内存.预先建立的这个物理内存的是windows自己的加载程序, ...