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讲稿的整理和注释
链接放在这里,有点难理解,至少我个人是的. 后缀自动机是一种有限状态自动机,其功能是识别字符串是否是母串的后缀.它能解决的问题当然不仅仅是判断是不是后缀这种事,跟字符串的连续子串有关的问题都可以往这个 ...
随机推荐
- oracle查看表中数据的大小
通过从视图 user_segments的字段 bytes中找到 select SUM(bytes)/1024/1024 from user_segments where segment_name='E ...
- Django【设计】同功能不同实现模式的兼容性
需求: 当我们采集硬件信息时,客户端可以有多种方式,具体方式取决于客户机,CMDB项目中,我们有三种方式可选,AGENT/SSH/SALT,根据客户机具体情况和“SALT>>SSH> ...
- [006] largest_common_substring
[Description] Given two different strings, find the largest successive common substring. e.g. str1[] ...
- Linux内核多线程实现方法 —— kthread_create函数【转】
转自:http://blog.csdn.net/sharecode/article/details/40076951 Linux内核多线程实现方法 —— kthread_create函数 内核经常需要 ...
- Linux下如何打开img镜像文件
有些镜像文件为IMG格式,在Linux如何打开呢?例如从微软dreampark下载的Windows Server 2008 R2镜像文件,使用file命令查看: $ file chs_windows_ ...
- Excel---导出与读取(大数据量)
Excel下载 首先大数据量的下载,一般的Excel下载操作是不可能完成的,会导致内存溢出 SXSSFWorkbook 是专门用于大数据了的导出 构造入参rowAccessWindowSize 这个参 ...
- learnyounode 题解
//第三题 var fs =require('fs')var path=process.argv[2]fs.readFile(path,function(err,data){ var lines=da ...
- go中操作json
package main import ( "encoding/json" "fmt" ) type Server struct { ServerName st ...
- you have to first modify the default Eclipse configuration to avoid XML cosmetic errors:
Configure XML Validation to Avoid Cosmetic Errors Navigate to: Window->Preferences->XML->XM ...
- Oracle与MySQL连接配置
MySQL: Driver: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/GBDSPT(数据库名称) Oracle: Driver:o ...