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 ...
随机推荐
- Android 开发环境安装配置手册
本文指导,如何一步步搭建Android开发平台. 1 下载软件 n JDK 1.5+ 到 http://java.sun.com/javase/downloads/index.jsp 下载 n ...
- 获取鼠标经过处的标签的标签名和id
<script> var el = window.document.body; // 声明一个变量,默认值为body window.document.body.onmouseover = ...
- luogu P1375 小猫(卡特兰数)
题意 (n<=200000) 题解 把DP转移方程写出来,这不是卡特兰数吗?然后就解决了. 做完这题我发现 DP真是一个好东西. (公式连乘所以中间要加mod要不爆longlong了) #inc ...
- BZOJ 4103 [Thusc 2015]异或运算 (可持久化01Trie+二分)
题目大意:给你一个长方形矩阵,位置$i,j$上的数是$a_{i}\;xor\;b_{j}$,求某个子矩阵内第$K$大的值 最先想的是二分答案然后验证,然而是$O(qnlogmloga_{i})$,不出 ...
- php5权限控制修饰符
1.public:public表明该数据成员.成员函数是对所有用户开放的,所有用户都可以直接进行调用 2.private:private表示私有,私有的意思就是除了class自己之外,任何人都不可以直 ...
- 学习爬虫:《Python网络数据采集》中英文PDF+代码
适合爬虫入门的书籍<Python网络数据采集>,采用简洁强大的Python语言,介绍了网络数据采集,并为采集新式网络中的各种数据类型提供了全面的指导.第一部分重点介绍网络数据采集的基本原理 ...
- 紫书 例题8-4 UVa 11134(问题分解 + 贪心)
这道题目可以把问题分解, 因为x坐标和y坐标的答案之间没有联系, 所以可以单独求两个坐标的答案 我一开始想的是按照左区间从小到大, 相同的时候从右区间从小到大排序, 然后WA 去uDebug找了数据 ...
- 使用JNA,让java调用原生代码
JNA定义: JNA:java Native Access,是SUN公司开发的基于JNI的框架.JNI使得Java可以调用原生的c或者c++代码. JNA与JNI(Java Native Interf ...
- [AngularJS] ng-ture-value & ng-false-value
When you have a checkbox, not necessary that just 'ture' & 'false', you might have 'yes', 'no', ...
- Asterisk[1]
Asterisk[1]是一款GPLv2协议下的开源电话应用平台.简单来说,Asterisk是一个server应用.可以完毕发起电话呼叫.接受电话呼叫.对电话呼叫进行定制处理. 1.2.1 通道驱动 a ...