SCOJ 4484 The Graver Robbers' Chronicles 后缀自动机
4484: The Graver Robbers' Chronicles
题目连接:
http://acm.scu.edu.cn/soj/problem.action?id=4484
Description
One day, Kylin Zhang and Wu Xie are trapped in a graveyard. They find an ancient piece of parchment with a cipher string.
After discussion and analysis, they find the password is the total number of the cipher string's distinct substrings.
Although Kylin Zhang is powerful and always help his friends get rid of danger, this time he is helpless beacause
he is not good at math and programming.
As a smart Acmer, can you help them solve this problem so that they can escape from this horrible graveyard.
Input
The first line is an integer T stands for the number of test cases.
Then T test case follow.
For each test case there is one string.
Constraints:
T is no bigger than 30.
The length of string is no bigger than 50000.
Every string only contains lowercase letters.
Output
For each test case, output the answer in a single line. one number saying the number of distinct substrings.
Sample Input
2
aaaaa
cacac
Sample Output
5
9
Hint
题意
给你一个串,问你有多少个不同的子串
题解:
后缀自动机/后缀数组裸题
枚举结尾的字符的最长后缀,减掉和自己父亲重合的位置就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 50100;
char s1[maxn];
struct SAM {
struct {
int len, f, ch[26];
void init() {
len = 0, f = -1;
memset(ch, 0xff, sizeof (ch));
}
} e[maxn<<1];
int idx, last;
void init() {
idx = last = 0;
e[idx++].init();
}
int newnode() {
e[idx].init();
return idx++;
}
void add(int c) {
int end = newnode();
int tmp = last;
e[end].len = e[last].len + 1;
for (; tmp != -1 && e[tmp].ch[c] == -1; tmp = e[tmp].f) {
e[tmp].ch[c] = end;
}
if (tmp == -1) e[end].f = 0;
else {
int nxt = e[tmp].ch[c];
if (e[tmp].len + 1 == e[nxt].len) e[end].f = nxt;
else {
int np = newnode();
e[np] = e[nxt];
e[np].len = e[tmp].len + 1;
e[nxt].f = e[end].f = np;
for (; tmp != -1 && e[tmp].ch[c] == nxt; tmp = e[tmp].f) {
e[tmp].ch[c] = np;
}
}
}
last = end;
}
};
SAM sam;
void solve()
{
scanf("%s",s1);
int len = strlen(s1);
sam.init();
for(int i=0;i<len;i++)
sam.add(s1[i]-'a');
long long ans = 0;
for(int i=1;i<sam.idx;i++)
ans = ans + sam.e[i].len - sam.e[sam.e[i].f].len;
printf("%lld\n",ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)solve();
return 0;
}
SCOJ 4484 The Graver Robbers' Chronicles 后缀自动机的更多相关文章
- SCOJ 4493: DNA 最长公共子串 后缀自动机
4493: DNA 题目连接: http://acm.scu.edu.cn/soj/problem.action?id=4493 Description Deoxyribonucleic acid ( ...
- BZOJ 后缀自动机四·重复旋律7
后缀自动机四·重复旋律7 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 神奇的 ...
- 【Codeforces235C】Cyclical Quest 后缀自动机
C. Cyclical Quest time limit per test:3 seconds memory limit per test:512 megabytes input:standard i ...
- 【hihocoder#1413】Rikka with String 后缀自动机 + 差分
搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...
- 【BZOJ-3998】弦论 后缀自动机
3998: [TJOI2015]弦论 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2018 Solved: 662[Submit][Status] ...
- HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- hihoCoder 后缀自动机三·重复旋律6
后缀自动机三·重复旋律6 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数列. 现在小Hi ...
- hihoCoder #1445 : 后缀自动机二·重复旋律5
#1445 : 后缀自动机二·重复旋律5 时间限制:10000ms 单点时限:2000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数 ...
- 数据结构:后缀自动机 WJMZBMR讲稿的整理和注释
链接放在这里,有点难理解,至少我个人是的. 后缀自动机是一种有限状态自动机,其功能是识别字符串是否是母串的后缀.它能解决的问题当然不仅仅是判断是不是后缀这种事,跟字符串的连续子串有关的问题都可以往这个 ...
随机推荐
- discuz2.5登录后台闪退的解决办法
今天突然发现discuz2.5论坛后台进不去,开始以为密码错了,但发现登录后也是闪退.我试着清除浏览器cookie,也换了其他浏览器也没有用,还是上网找找吧! discuz2.5进入后台闪退的原因: ...
- ThinkPHP的运行流程-1
我在index\Lib\Action\目录下新建了一个ShowAction.class.php文件.ps:该目录是控制器的目录. 然后这个文件中继承了action这个类.代码如下: 1 2 3 4 5 ...
- SurfaceFlinger 讲解
SurfaceFlinger是Android multimedia的一个部分,在Android 的实现中它是一个service,提供系统 范围内的surface composer功能,它能够将各种应用 ...
- 2017 ACM ICPC Asia Regional - Daejeon
2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...
- oracle命令生成AWR报告
--命令生成AWR报告oracle@linux:~> sqlplus / as sysdba SQL*Plus: Release 11.1.0.7.0 - Production on Fri A ...
- 美化的select下拉框
ie7浏览器以后的下拉框,给他加上边框样式,是没用的.要是想做出样式好看的下拉框需要用js或者jquery来模拟实现. 代码如下: <div class="r"> &l ...
- caffe多个gpu数据合并到一起
当多GPU树形拓扑构建完毕,数据预缓冲到GPU显存,开始进入多GPU并行训练.Caffe的Solver提供了两个用于多GPU训练的回调函数:on_start()和on_gradient_ready() ...
- es 插件
类 若 实现NativeScriptFactory接口.A factory to create instances of either {@link ExecutableScript} or {@li ...
- vue单文件中scoped样式如何穿透?
在vue文件中的style标签上,有一个特殊的属性:scoped.当一个style标签拥有scoped属性时,它的CSS样式就只能作用于当前的组件,也就是说,该样式只能适用于当前组件元素.通过该属性, ...
- 将dubbo框架里的服务提供者迁移进k8s里,以docker提供服务时要注意的细节
在将dubbo框架里的服务提供者迁移进k8s时,有几个注意事项: 1, 要为默认情况下,dubbo会将k8s里的pod ip地址作为注册服务的地址,所以这个地址需要被改写(ip测试过,行得通,而dns ...