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 ...
随机推荐
- angular 报错笔记
1.错误信息: Failed to instantiate module app due to: Error: [$injector:unpr] http://errors.angularjs.org ...
- hdu 2037 - 典型贪心*
题目链接 给一堆电视节目的起止时间,问最多能完整收看几个节目 --------------------------------------------------------------------- ...
- vmware workstation中的NAT配置
宿主机:win10: IP:192.168.1.101 GW:192.168.1.1 以太网2(VMNET8) IP:192.168.100.1 GW:nonevmware中的虚拟网络设置(NAT): ...
- day02变量
一. 什么是变量? 变量:在程序运行过程中,值会发生变化的量.(与之相对应的,常量就是在程序运行过程中,值不会发生变化的量).无论是变量还是常量,在创建时都会在内存中开辟一块空间,用于保存它的值. 二 ...
- 小程序QQ版表情解析组件
代码片段: [https://developers.weixin.qq.com/s/KLaD5MmD7V45) GitHub: https://github.com/WozHuang/Miniprog ...
- spring慕课网
资源链接 http://spring.io/ http://projects.spring.io/spring-framework/ Spring是什么? Spring是一个开源的轻量级的应用开发框架 ...
- Virtualenv入门基础教程
本文目录: [TOC]虚拟环境简介 VirtualEnv用于在一台机器上创建多个独立的Python虚拟运行环境,多个Python环境相互独立,互不影响,它能够: 在没有权限的情况下安装新套件 不 ...
- 总结使人进步,可视化界面GUI应用开发总结:Android、iOS、Web、Swing、Windows开发等
可视化界面的软件,是21世纪最主流的应用类型了,黑屏控制台的不适合普通用户. 2004年左右的时候,作为普通网民,接触的自然是可视化,准确是Windows那一套. 那个时候,Microsoft ...
- hook中ref使用
hook使用ref 父组件: 引入 useRef 声明ref的名字 const dateRef = useRef() 复值给组件 ref={d ...
- Axure7.0在OS X Yosemite(10.10)中不能用的问题
电脑升级到了10.10后发现axure7.0不能使用.解决办法也非常easy,就是又一次下载一个新的axure版本号,下载地址:http://www.axure.com/release-candida ...