给定一个字符串,求不相同子串个数。
每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同子串个数。
总数为n*(n-1)/2,再减掉height[i]的和就是答案

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e6+;
int sa[maxn];
int t1[maxn];
int t2[maxn];
int c[maxn];
int rk[maxn];
int height[maxn];
void build_sa (int s[],int n,int m) {
int i,j,p;
int *x=t1,*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 (j=;j<=n;j<<=) {
p=;
for (i=n-j;i<n;i++) y[p++]=i;
for (i=;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;
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);
p=;
x[sa[]]=;
for (i=;i<n;i++)
x[sa[i]]=y[sa[i-]]==y[sa[i]]&&y[sa[i-]+j]==y[sa[i]+j]?p-:p++;
if (p>=n) break;
m=p;
}
}
void getHeight (int s[],int n) {
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 (s[i+k]==s[j+k]) k++;
height[rk[i]]=k;
}
}
bool check (int n,int k,int t) {
int num=;
for (int i=;i<=n;i++) {
if (height[i]>=t) {
num++;
if (num>=k) return true;
}
else num=;
}
return false;
}
char str[maxn];
int s[maxn];
int main () {
int T;
scanf ("%d",&T);
while (T--) {
scanf ("%s",str);
int n=strlen(str);
for (int i=;i<=n;i++) s[i]=str[i];
build_sa(s,n+,);
getHeight(s,n);
int ans=n*(n+)/;
for (int i=;i<=n;i++) ans-=height[i];
printf ("%d\n",ans);
}
return ;
}

SPOJ Distinct Substrings的更多相关文章

  1. SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)

    DISUBSTR - Distinct Substrings no tags  Given a string, we need to find the total number of its dist ...

  2. SPOJ Distinct Substrings【后缀数组】

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  3. spoj - Distinct Substrings(后缀数组)

    Distinct Substrings 题意 求一个字符串有多少个不同的子串. 分析 又一次体现了后缀数组的强大. 因为对于任意子串,一定是这个字符串的某个后缀的前缀. 我们直接去遍历排好序后的后缀字 ...

  4. SPOJ - Distinct Substrings,求不同的字串个数!

    DISUBSTR - Distinct Substrings 题意:给你一个长度最多1000的字符串,求不相同的字串的个数. 思路:一个长度为n的字符串最多有(n+1)*n/2个,而height数组已 ...

  5. SPOJ Distinct Substrings SA

    正解:SA 解题报告: 传送门! 啊先给个翻译趴QwQ大概就是说给个字符串,求互不相等的子串的个数 算是道小水题辣趴,,,并不难想到的呢QAQ只是因为是新知识所以巩固下而已QAQ 然后就显然考虑合法方 ...

  6. spoj Distinct Substrings 后缀数组

    给定一个字符串,求不相同的子串的个数. 假如给字符串“ABA";排列的子串可能: A B A AB  BA ABA 共3*(3+1)/2=6种; 后缀数组表示时: A ABA BA 对于A和 ...

  7. SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转

    694. Distinct Substrings Problem code: DISUBSTR   Given a string, we need to find the total number o ...

  8. 【SPOJ】Distinct Substrings(后缀自动机)

    [SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...

  9. 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)

    [SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...

随机推荐

  1. 油候插件grant的使用

    // ==UserScript== // @name Test Baidu // @namespace http://www.baidu.com/ // @version 0.1 // @descri ...

  2. actiBPM插件的办法

    1.下载actiBPM到本地 从IDEA官网下载actiBPM.jar包 IDEA官网:https://plugins.jetbrains.com/ 官网搜索actiBPM 2.从本地安装actiBP ...

  3. wcf编程资料

    如下为WCF编辑资料 链接:https://pan.baidu.com/s/1kZnc6eNOfEggHSfJNXj8Ag 提取码:gj7s 复制这段内容后打开百度网盘手机App,操作更方便哦 第01 ...

  4. LED Holiday Light - Color Creation Of LED Christmas Lights

    In the long and interesting history of Christmas lights, nothing is brighter than light-emitting dio ...

  5. python3练习100题——010

    第10天了,今天的题目跟009类似,都比较水,有时间的话再做一道- 链接:http://www.runoob.com/python/python-exercise-example10.html 题目: ...

  6. Auto.js的初次使用——在VSCode中使用

    最近双十一大家都在集猫币,盖楼,但是每天刷任务太浪费时间了.被推荐了一个脚本可以自动刷任务,很是好奇.于是想要了解一下Auto.js 一.vscode启动Auto.js 1.vscode里安装auto ...

  7. MySQL起别名

    好处: 便于理解 连接查询的时候,如果要查询的字段有重名的情况,使用别名可以区分开来 注意: 如果别名中有特殊符号 # 空格 ... ,需要用 "双引号" 把别名引起来单引号也行, ...

  8. borderInterpolate()函数

    官网:borderInterpolate borderInterpolate 函数原型 int borderInterpolate( int p, int len, int borderType ); ...

  9. 通过java代码HttpRequestUtil(服务器端)发送HTTP请求并解析

    关键代码:String jsonStr = HttpRequestUtil.sendGet(config.getAddress() + config.getPorts() + config.getFi ...

  10. python的setup和teardown

    关于python unittest ① setup():每个测试函数运行前运行② teardown():每个测试函数运行完后执行③ setUpClass():必须使用@classmethod 装饰器, ...