题目链接 :  SOJ-4484

The Graver Robbers' Chronicles


Description

One day, Kylin Zhang and Wu Xie are trapped in a graveyard. They find an ancient piece of parchment with a cipher string. After discussion and analysis, they find the password is the total number of the cipher string's distinct substrings.Although Kylin Zhang is powerful and always help his friends get rid of danger, this time he is helpless beacause he is not good at math and programming. As a smart Acmer, can you help them solve this problem so that they can escape from this horrible graveyard.

Input

The first line is an integer T stands for the number of test cases.
Then T test case follow.
For each test case there is one string.Constraints:
T is no bigger than 30.
The length of string is no bigger than 50000.
Every string only contains lowercase letters.

Output

For each test case, output the answer in a single line. one number saying the number of distinct substrings.

Sample Input

2
aaaaa
cacac

Sample Output

5
9

Hint

For test case 2, the distinct substrings of 'cacac' are as follows:
len = 1 : c , a
len = 2 : ca , ac
len = 3 : cac , aca
len = 4 : caca , acac
len = 5 : cacac
Thus, total number of distinct substrings is 9.

Author

_L

题目大意:给你一个字符串,求出有多少种不同的子串(连续)包括本身  见样例

题目思路:  看了很久的后缀数组了,,一直都不太懂怎么来的,之后用模板处理一些简单的问题,,这道题关键是对后缀数组height数组的理解,,具体我这里就不描述了,,答案就是所有的子串的个数减去height数组里的值,,这里会超int,,所以要用long long

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;const int maxn = 1e5;int cntA[maxn],sa[maxn],Rank[maxn],cntB[maxn],height[maxn],A[maxn],B[maxn],tsa[maxn];
char ch[maxn];
int n;
void solve()
{for (int i = 0; i < 256; i ++) cntA[i] = 0;for (int i = 1; i <= n; i ++) cntA[ch[i-1]] ++;for (int i = 1; i < 256; i ++) cntA[i] += cntA[i - 1];for (int i = n; i; i --) sa[cntA[ch[i-1]] --] = i;Rank[sa[1]] = 1;for (int i = 2; i <= n; i ++){Rank[sa[i]] = Rank[sa[i - 1]];if (ch[sa[i]-1] != ch[sa[i - 1]-1]) Rank[sa[i]] ++;}for (int l = 1; Rank[sa[n]] < n; l <<= 1){for (int i = 0; i <= n; i ++) cntA[i] = 0;for (int i = 0; i <= n; i ++) cntB[i] = 0;for (int i = 1; i <= n; i ++){cntA[A[i] = Rank[i]] ++;cntB[B[i] = (i + l <= n) ? Rank[i + l] : 0] ++;}for (int i = 1; i <= n; i ++) cntB[i] += cntB[i - 1];for (int i = n; i; i --) tsa[cntB[B[i]] --] = i;for (int i = 1; i <= n; i ++) cntA[i] += cntA[i - 1];for (int i = n; i; i --) sa[cntA[A[tsa[i]]] --] = tsa[i];Rank[sa[1]] = 1;for (int i = 2; i <= n; i ++){Rank[sa[i]] = Rank[sa[i - 1]];if (A[sa[i]] != A[sa[i - 1]] || B[sa[i]] != B[sa[i - 1]]) Rank[sa[i]] ++;}}for (int i = 1, j = 0; i <= n; i ++){if (j) j --;while (ch[i + j-1] == ch[sa[Rank[i] - 1] + j-1]) j ++;height[Rank[i]] = j;}
}int main()
{int t;cin>>t;while(t--){scanf("%s",ch);n=strlen(ch);solve();long long ans = (long long)(1+n)*n/2;for(int i=1;i<=n;i++)ans-=(long long)height[i];cout<<ans<<endl;}return 0;
}

SOJ-4484-后缀数组相关推荐

  1. 寻找一个字符串的重复子串 后缀数组

    什么是后缀数组 令字符串 S=S[1]S[2]...S[n]S=S[1]S[2]...S[n]{\displaystyle S=S[1]S[2]...S[n]} , S[i,j]S[i,j]{\dis ...

  2. 【2012百度之星/资格赛】H:用户请求中的品牌 [后缀数组]

    时间限制: 1000ms 内存限制: 65536kB 描述 馅饼同学是一个在百度工作,做用户请求(query)分析的同学,他在用户请求中经常会遇到一些很奇葩的词汇.在比方说"johnsonj ...

  3. Boring counting HDU - 3518 (后缀数组)

    Boring counting \[ Time Limit: 1000 ms \quad Memory Limit: 32768 kB \] 题意 给出一个字符串,求出其中出现两次及以上的子串个数,要 ...

  4. HDU4080 Stammering Aliens(二分 + 后缀数组)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4080 Description Dr. Ellie Arroway has establish ...

  5. 后缀数组 + Hash + 二分 or Hash + 二分 + 双指针 求 LCP ---- 2017icpc 青岛 J Suffix (假题!!)

    题目链接 题目大意: 就是给你n个串每个串取一个后缀,要求把串拼起来要求字典序最小!! sum_length_of_n≤5e5sum\_length\_of\_n\leq 5e5sum_length_ ...

  6. 后缀数组 ---- 2018~2019icpc焦作H题[后缀数组+st表+二分+单调栈]

    题目链接 题目大意: 给出nnn个数,定义f[l,r]f[l,r]f[l,r]表示 区间[l,r][l,r][l,r]的最大值,求所有 子区间的最大值的和,要求相同的子区间只能算一次 比如数列 5 6 ...

  7. [Ahoi2013]差异[后缀数组+单调栈]

    链接 解题思路:很明显前面∑1<=i<j<=nlen(Ti)+len(Tj)\sum_{1<=i<j<=n}len(T_i)+len(T_j)∑1<=i< ...

  8. poj2217详解 ( 后缀数组 + 高度数组 )

    题目大概意思就是 给两个字符串,求最长公共字符串子串长度 我们可以考虑用后缀数组和高度数组 一个字符串 中 最长的两个相同字符串长度, 不就是 后缀数组中相邻两个后缀的最长公共前缀, 不就是 高度数组 ...

  9. 树链剖分 + 后缀数组 - E. Misha and LCP on Tree

    E. Misha and LCP on Tree Problem's Link Mean: 给出一棵树,每个结点上有一个字母.每个询问给出两个路径,问这两个路径的串的最长公共前缀. analyse: ...

  10. BZOJ3473:字符串(后缀数组,主席树,二分,ST表)

    Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串. Output 一 ...

最新文章

  1. html流式布局怎么用,css 流式布局什么意思?
  2. 英国推6.5亿英镑网络安全战略 强化安全屏障
  3. 1.arm的linux系统搭建
  4. 深度学习笔记 第四门课 卷积神经网络 第三周 目标检测
  5. 城步一中2021高考成绩查询,2017城步一中录取分数线(附高考成绩喜报)
  6. My team and my group in my opportunity and my appointment
  7. python 可执行文件_python调用可执行文件的方法
  8. 深度学习之LSTM完全图解
  9. 获取实时股票行情通达信接口
  10. 英语对程序员来讲有多重要?不会英语可以做程序员吗?
  11. 网页中自动连续播放音乐文件的实现
  12. Linux——clamAV查杀病毒与防护
  13. 如何在Mac Finder中查找/Usr 路径?
  14. oracle的安装教程
  15. ubuntu 20.04 设置DNS
  16. dashboard android,Dashboard软件下载
  17. int i ; const int val=i ; 在C++中这种赋值对吗?
  18. 计算机键盘分为哪5个区,计算机键盘分区之功能键区
  19. 中心差商公式不同的h计算近似一(二)阶导数
  20. “安心记加班”完成亿元级B轮融资,蚂蚁金服领投

热门文章

  1. 【ajax】7.请求:请求超时与网络异常处理+取消请求+请求重复发送问题
  2. 数据分析师应该怎么优化自己的简历?
  3. [VT虚拟化驱动]启用EPT
  4. 二维码制作教程分享,大家一起来学习吧!
  5. 如何用Dreamweaver编辑rails的html.erb文件
  6. Re-ID Driven Localization Refinement for Person Search
  7. 《艺多不压身 艺多不养家》反思
  8. VMware和mobaXterm连接总结
  9. VMware winserver2016安装
  10. 联通速品简易测试报告