SPOJ 694 Distinct Substrings
Distinct Substrings
This problem will be judged on SPOJ. Original ID: DISUBSTR
64-bit integer IO format: %lld Java class name: Main
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.
解题:求不同子串的个数,n - sa[i] - 1表示当前后缀的前缀个数,然后减去 height[i],也就是减去与前面的重复的。
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int sa[maxn],rk[maxn],height[maxn];
int c[maxn],t[maxn],t2[maxn],n;
char s[maxn];
void build_sa(int m) {
int i,j,*x = t,*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(int k = ; k <= n; k <<= ) {
int p = ;
for(i = n-k; i < n; ++i) y[p++] = i;
for(i = ; i < n; ++i)
if(sa[i] >= k) y[p++] = sa[i] - k;
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);
x[sa[]] = ;
p = ;
for(i = ; i < n; ++i)
if(y[sa[i]] == y[sa[i-]] && y[sa[i]+k] == y[sa[i-]+k])
x[sa[i]] = p-;
else x[sa[i]] = p++;
if(p >= n) break;
m = p;
}
}
void getHeight(){
int i,j,k = ;
for(i = ; i < n; ++i) rk[sa[i]] = i;
for(i = ; i < n; ++i){
if(k) --k;
j = sa[rk[i]-];
while(i+k<n&&j+k<n&&s[i+k]==s[j+k]) ++k;
height[rk[i]] = k;
}
}
int main() {
int kase;
scanf("%d",&kase);
while(kase--){
scanf("%s",s);
n = strlen(s) + ;
build_sa();
getHeight();
int ret = ;
for(int i = ; i < n; ++i)
ret += n - sa[i] - height[i] - ;
printf("%d\n",ret);
}
return ;
}
后缀自动机
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
char str[maxn];
int ret,n;
struct SAM{
struct node{
int son[],f,len;
void init(){
f = -;
len = ;
memset(son,-,sizeof son);
}
}e[maxn];
int tot,last;
int newnode(int len = ){
e[tot].init();
e[tot].len = len;
return tot++;
}
void init(){
tot = last = ;
newnode();
}
void extend(int c){
int p = last,np = newnode(e[p].len + );
while(p != - && e[p].son[c] == -){
e[p].son[c] = np;
p = e[p].f;
}
if(p == -) e[np].f = ;
else{
int q = e[p].son[c];
if(e[p].len + == e[q].len) e[np].f = q;
else{
int nq = newnode();
e[nq] = e[q];
e[nq].len = e[p].len + ;
e[q].f = e[np].f = nq;
while(p != - && e[p].son[c] == q){
e[p].son[c] = nq;
p = e[p].f;
}
}
}
last = np;
}
void solve(){
ret = ;
for(int i = ; i < tot; ++i)
ret += e[i].len - e[e[i].f].len;
printf("%d\n",ret);
}
}sam; int main(){
int kase;
scanf("%d",&kase);
while(kase--){
scanf("%s",str);
n = strlen(str);
sam.init();
for(int i = ret = ; str[i]; ++i)
sam.extend(str[i]-'A');
sam.solve();
}
return ;
}
SPOJ 694 Distinct Substrings的更多相关文章
- SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- SPOJ 694 Distinct Substrings/SPOJ 705 New Distinct Substrings(后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- spoj 694. Distinct Substrings 后缀数组求不同子串的个数
题目链接:http://www.spoj.com/problems/DISUBSTR/ 思路: 每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数.如果所有的后缀按照su ...
- 后缀数组 SPOJ 694 Distinct Substrings
题目链接 题意:给定一个字符串,求不相同的子串的个数 分析:我们能知道后缀之间相同的前缀的长度,如果所有的后缀按照 suffix(sa[0]), suffix(sa[1]), suffix(sa[2] ...
- SPOJ 694 Distinct Substrings(不相同子串个数)
https://vjudge.net/problem/SPOJ-DISUBSTR 题意: 给定一个字符串,求不相同的子串的个数. 思路: #include<iostream> #inclu ...
- 【SPOJ】Distinct Substrings(后缀自动机)
[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...
- 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)
[SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...
- 【SPOJ】Distinct Substrings
[SPOJ]Distinct Substrings 求不同子串数量 统计每个点有效的字符串数量(第一次出现的) \(\sum\limits_{now=1}^{nod}now.longest-paren ...
- SPOJ - DISUBSTR Distinct Substrings (后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
随机推荐
- POJ 2431 Expedition (priority_queue或者multiset可解)
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18655 Accepted: 5405 Descr ...
- PostgreSQL+PostGIS
PostGIS简介 PostGIS是对象关系型数据库系统PostgreSQL的一个扩展,PostGIS提供如下空间信息服务功能:空间对象.空间索引.空间操作函数和空间操作符.同时,PostGIS遵循O ...
- 网页结构的简介和Xpath语法的入门教程
相信很多小伙伴已经听说过Xpath,之前小编也写过一篇关于Xpath的文章,感兴趣的小伙伴可以戳这篇文章如何利用Xpath抓取京东网商品信息以及Python网络爬虫四大选择器(正则表达式.BS4.Xp ...
- (2016北京集训十三)【xsy1532】网络战争 - 最小割树+树上倍增+KD树
题解: 好题!! 这题似乎能上我代码长度记录的前五? 调试时间长度应该也能上前五QAQ 首先题目要求的明显就是最小割,当然在整个森林上求Q次最小割肯定是会GG的,所以我们需要一个能快速求最小割的算法— ...
- 四则运算1 java+jsp+SQLServer
1,设计思想(1)在java resourse里定义包和类 (2)在类里定义生成算式,并将算式保存在数据库中的方法 (3)在jsp文件中调用java方法 2,源程序代码 生成算式的方法 public ...
- 超简单入门Vuex小示例
写在前面 本文旨在通过一个简单的例子,练习vuex的几个常用方法,使初学者以最快的速度跑起来一个vue + vuex的示例. 学习vuex需要你知道vue的一些基础知识和用法.相信点开本文的同学都具备 ...
- unity Android在streamingAssets路径下文件无法读取的的解决方法
unity Android在streamingAssets路径下文件,有时候plugin下的.jar或者.so无法直接读取: 解决方法之一,拷贝至其他路径: #if UNITY_ANDROID str ...
- PatentTips - Controlling voltage and frequency
BACKGROUND OF THE INVENTION Mobile devices, such as but not limited to personal data appliances, cel ...
- Qt之QFileIconProvider
简述 QFileIconProvider类为QDirModel和QFileSystemModel类提供了文件图标. 简述 共有类型 公共函数 示例 IconType 效果 源码 QFileInfo 效 ...
- Tsinsen A1303. tree(伍一鸣) LCT
LCT的各种操作... . cut link add mul size rev query 写的效率不够高... BZOJ上似乎TLE. ... A1303. tree(伍一鸣) 时间限制:2.5s ...