SPOJ705 Distinct Substrings (后缀自动机&后缀数组)
Given a string, we need to find the total number of its distinct substrings.
Input
T- number of test cases. T<=20;
Each test case consists of one string, whose length is <= 1000
Output
For each test case output one number saying the number of distinct substrings.
Example
Sample Input:
2
CCCCC
ABABA
Sample Output:
5
9
Explanation for the testcase with string ABABA:
len=1 : A,B
len=2 : AB,BA
len=3 : ABA,BAB
len=4 : ABAB,BABA
len=5 : ABABA
Thus, total number of distinct substrings is 9.
题意:
求出大写的字符串里不同的子串。默写了一遍后缀自动机。今天主要是练习后缀数组。
注意:
- 注意是大写还是小写;
- 注意init初始化的时候没有一次性memset,所以下面要把每个新出现的点memset。不要搞忘。
后缀自动机:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
struct SAM
{
int ch[maxn][],fa[maxn],maxlen[maxn],Last,sz;
void init()
{
sz=Last=; fa[]=maxlen[]=;
memset(ch[],,sizeof(ch[]));
}
void add(int x)
{
int np=++sz,p=Last;Last=np;
memset(ch[np],,sizeof(ch[np]));
maxlen[np]=maxlen[p]+;
while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p];
if(!p) fa[np]=;
else {
int q=ch[p][x];
if(maxlen[p]+==maxlen[q]) fa[np]=q;
else {
int nq=++sz;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
maxlen[nq]=maxlen[p]+;
fa[nq]=fa[q];
fa[q]=fa[np]=nq;
while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p];
}
}
}
};
SAM Sam;
int main()
{
char chr[maxn];
int T,ans,i,L;
scanf("%d",&T);
while(T--){
Sam.init();ans=;
scanf("%s",chr);
L=strlen(chr);
for(i=;i<L;i++) Sam.add(chr[i]-'A');
for(i=;i<=Sam.sz;i++) ans+=Sam.maxlen[i]-Sam.maxlen[Sam.fa[i]];
printf("%d\n",ans);
}
return ;
}
后缀数组:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
char ch[maxn];
int L;
struct SA
{
int cntA[maxn],cntB[maxn],A[maxn],B[maxn];
int rank[maxn],sa[maxn],tsa[maxn],ht[maxn];void sort()
{
for (int i = ; i < ; i ++) cntA[i] = ;
for (int i = ; i <= L; i ++) cntA[ch[i]] ++;
for (int i = ; i < ; i ++) cntA[i] += cntA[i - ];
for (int i = L; i; i --) sa[cntA[ch[i]] --] = i;
rank[sa[]] = ;
for (int i = ; i <= L; i ++){
rank[sa[i]] = rank[sa[i - ]];
if (ch[sa[i]] != ch[sa[i - ]]) rank[sa[i]] ++;
}
for (int l = ; rank[sa[L]] < L; l <<= ){
for (int i = ; i <= L; i ++) cntA[i] = ;
for (int i = ; i <= L; i ++) cntB[i] = ;
for ( int i = ; i <= L; i ++){
cntA[A[i] = rank[i]] ++;
cntB[B[i] = (i + l <= L) ? rank[i + l] : ] ++;
}
for (int i = ; i <= L; i ++) cntB[i] += cntB[i - ];
for (int i = L; i; i --) tsa[cntB[B[i]] --] = i;
for (int i = ; i <= L; i ++) cntA[i] += cntA[i - ];
for (int i = L; i; i --) sa[cntA[A[tsa[i]]] --] = tsa[i];
rank[sa[]] = ;
for (int i = ; i <= L; i ++){
rank[sa[i]] = rank[sa[i - ]];
if (A[sa[i]] != A[sa[i - ]] || B[sa[i]] != B[sa[i - ]]) rank[sa[i]] ++;
}
}
}
void getheight()
{
for (int i = , j = ; i <= L; i ++){
if (j) j --;
while (ch[i + j] == ch[sa[rank[i] - ] + j]) j ++;
ht[rank[i]] = j;
}
}
};
SA Sa;
int main()
{
int T,ans,i;
scanf("%d",&T);
while(T--){
ans=;
scanf("%s",ch+);
L=strlen(ch+);
Sa.sort();
Sa.getheight();
for(i=;i<=L;i++) ans+=L-Sa.sa[i]+-Sa.ht[i];
printf("%d\n",ans);
}
return ;
}
SPOJ705 Distinct Substrings (后缀自动机&后缀数组)的更多相关文章
- poj 1743 Musical Theme 后缀自动机/后缀数组/后缀树
题目大意 直接用了hzwer的题意 题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题."主题&qu ...
- [模板] 后缀自动机&&后缀树
后缀自动机 后缀自动机是一种确定性有限状态自动机, 它可以接收字符串\(s\)的所有后缀. 构造, 性质 翻译自毛子俄罗斯神仙的博客, 讲的很好 后缀自动机详解 - DZYO的博客 - CSDN博客 ...
- Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))
Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...
- Spoj-DISUBSTR - Distinct Substrings~New Distinct Substrings SPOJ - SUBST1~(后缀数组求解子串个数)
Spoj-DISUBSTR - Distinct Substrings New Distinct Substrings SPOJ - SUBST1 我是根据kuangbin的后缀数组专题来的 这两题题 ...
- Distinct Substrings SPOJ - DISUBSTR 后缀数组
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- 705. New Distinct Substrings spoj(后缀数组求所有不同子串)
705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...
- SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组
SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...
- 回文树&后缀自动机&后缀数组
KMP,扩展KMP和Manacher就不写了,感觉没多大意思. 之前感觉后缀自动机简直可以解决一切,所以不怎么写后缀数组. 马拉车主要是通过对称中心解决问题,有的时候要通过回文串的边界解决问题 ...
- POJ3080 POJ3450Corporate Identity(广义后缀自动机||后缀数组||KMP)
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
随机推荐
- Java引用类型作为形参和返回值
一.什么是引用类型 在Java中引用类型包括三种:类.抽象类.接口. 二.引用类型作为形参使用 1.类作为形参 /** * 类作为形参,实际传递的是该类的对象 */ class Student { p ...
- Android平台录音音量计的实现
今天博主要给大家分享的是怎样在Android平台上实现录音时的音量指示计.开门见山.先来看一张Demo的效果图: 如上图所看到的,两个button各自是開始录音和停止录音,中间的两个数字前后分别代表音 ...
- poj3708(公式化简+大数进制装换+线性同余方程组)
刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...
- 【BZOJ3769】spoj 8549 BST again DP(记忆化搜索?)
[BZOJ3769]spoj 8549 BST again Description 求有多少棵大小为n的深度为h的二叉树.(树根深度为0:左右子树有别:答案对1000000007取模) Input 第 ...
- Zipper (DP)
Zipper Given three strings, you are to determine whether the third string can be formed by combining ...
- 九度OJ 1349:数字在排序数组中出现的次数 (排序、查找)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2489 解决:742 题目描述: 统计一个数字在排序数组中出现的次数. 输入: 每个测试案例包括两行: 第一行有1个整数n,表示数组的大小. ...
- 九度OJ 1195:最长&最短文本 (搜索)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3144 解决:1156 题目描述: 输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输 ...
- 搭建Cat笔记01
昨天晚上搭建Cat 时候那叫一个坑b,宝宝心里苦呀! 准备工作: 1.先大众点评Cat的项目源码,https://github.com/dianping/cat.git 2.打包编译: mvn cle ...
- 让Editplus支持sql语法高亮显示
版权声明:作者:jiankunking 出处:http://blog.csdn.net/jiankunking 本文版权归作者和CSDN共同拥有,欢迎转载.但未经作者允许必须保留此段声明,且在文章页面 ...
- statu 设置
DATA: itab TYPE TABLE OF sy-ucomm. APPEND 'DELE' TO itab. APPEND 'PICK' TO itab. SET PF-STATUS 'STA3 ...