题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 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, who…
题意: 问给定串有多少本质不同的子串? 思路: 子串必是某一后缀的前缀,假如是某一后缀\(sa[k]\),那么会有\(n - sa[k] + 1\)个前缀,但是其中有\(height[k]\)个和上一个重复,那么最终的贡献的新串为\(n - sa[k] + 1 - height[k]\).故最终结果为\(\sum_{i = 1}^n (n - sa[k] + 1 - height[k])\),即 \(\frac{n * (n + 1)}{2} - \sum_{i = 1}^nheight[k]\…
题意 : 对于给出的串,输出其不同长度的子串的种类数 分析 : 有一个事实就是每一个子串必定是某一个后缀的前缀,换句话说就是每一个后缀的的每一个前缀都代表着一个子串,那么如何在这么多子串or后缀的前缀中找出不同的并计数呢?思路就是所有的可能子串数 - 重复的子串数.首先我们容易得到一个长度为 len 的串的子串数为 len * ( len + 1) / 2.那如何知道重复的子串数呢?答案就是利用后缀数组去跑一遍 Height ,得到所有的最长公共前缀(LCP),这些最长公共前缀的值都存在了 He…
题目链接:http://www.spoj.com/problems/DISUBSTR/ 思路: 每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数.如果所有的后缀按照suffix(sa[1]),suffix(sa[2]),suffix(sa[3]),……suffix(sa[n])的顺序计算,不难发现,对于每一次新加进来的后缀suffix(sa[k]),它将产生n-sa[k]+1个新的前缀.但是其中有height[k]个是和前面的字符串的前缀是相同的.所以suffix…
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 <= 50000 Output For each test case output one number saying the number of disti…
New Distinct Substrings 题意 给出T个字符串,问每个字符串有多少个不同的子串. 思路 字符串所有子串,可以看做由所有后缀的前缀组成. 按照后缀排序,遍历后缀,每次新增的前缀就是除了 与上一个后缀的所有公共前缀 之外的前缀. 答案就是用总数-重复的 即\(\frac{n(n+1)}{2}-\sum_{i=1}^{n}height[i]\) 代码 // #include <bits/stdc++.h> #include <stdio.h> #include &l…
DISUBSTR - Distinct Substrings no tags  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 outpu…
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 distinc…
题意:统计母串中包含多少不同的子串 然后这是09年论文<后缀数组——处理字符串的有力工具>中有介绍 公式如下: 原理就是加上新的,减去重的,这题是因为打多校才补的,只能说我是个垃圾 #include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <cstdlib> #include <string> #include &l…
给定一个字符串,求不相同的子串的个数. 假如给字符串“ABA";排列的子串可能: A B A AB  BA ABA 共3*(3+1)/2=6种; 后缀数组表示时: A ABA BA 对于A和AB height[i]=1; 表明一个长度公共,所以ABA中多出现了A这个子串,所以6-1=5: 对于ABA BA height[i]=0,所以不需要减去. 最后答案为5: #include<iostream> #include<stdio.h> #include<string…
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 <= 50000 Output For each test case output one number saying the number of disti…
题目链接:https://vjudge.net/contest/70655#problem/C 后缀数组的又一神奇应用.不同子串的个数,实际上就是所有后缀的不同前缀的个数. 考虑所有的后缀按照rank排好了,我们现在已知height,也就是相邻的两个的最长公共前缀是多少.那么不同的子串个数怎么统计呢? 从第一个串开始考虑,ans+=L1.再看第二个串,会加进来几个不同的前缀呢?就是ans+=L2-height[2].第三个类似,会加进来ans+=L3-height[3]…… 因此最后的结果就是a…
DISUBSTR - Distinct Substrings 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 numb…
题意:求字符串中不同子串的个数. 解题关键:每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数. 1.总数减去height数组的和即可. 注意这里height中为什么不需要进行组合计数,因为,每一个height的左端点已经确定,所以只需变动右端点,总共$height[i]$种情况. 2.如果所有的后缀按照 suffix(sa[1]), suffix(sa[2]),suffix(sa[3]), …… ,suffix(sa[n])的顺序计算,不难发现,对于每一次新加进来…
SAM里的转台不会有重复串,所以答案就是每个right集合所代表的串个数的和 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=100005; int T,n,fa[N],ch[N][27],dis[N],cur=1,con=1,la; long long ans; char s[N]; void ins(int c,int id) { la=cu…
SPOJ_705_New Distinct Substrings_后缀数组 题意: 给定一个字符串,求该字符串含有的本质不同的子串数量. 后缀数组的一个小应用. 考虑每个后缀的贡献,如果不要求本质不同那就是n-sa[i]-1. 然后需要去重,就是把height[i]这部分减掉. 代码: #include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> using n…
后缀数组,看到网上很多题解都是单调栈,这里提供一个不是单调栈的做法, 首先将两个串 连接起来求height   求完之后按height值从大往小合并.  height值代表的是  sa[i]和sa[i-1] 的公共前缀长度,那么每次合并就是合并  i和i-1 那么在合并小的时候公共前缀更大的肯定已经都合并在一起,那么就可以直接统计了. #include<iostream> #include<cstdio> #include<algorithm> #include<…
http://www.spoj.com/problems/SUBST1/en/  题目链接 SUBST1 - New Distinct Substrings no tags  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 le…
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 <= 50000 Output For each test case output one number saying the number of disti…
给一个字符串求有多少个不相同子串. 每一个子串一定都是某一个后缀的前缀.由此可以推断出总共有(1+n)*n/2个子串,那么下面的任务就是找这些子串中重复的子串. 在后缀数组中后缀都是排完序的,从sa[1]到sa[n],这么思考以某个串为前缀的子串有几个,那么容易想到重复子串的个数其实就是∑height[i]. 所以结果就是(1+n)*n/2-∑height[i]. #include<cstdio> #include<cstring> #include<algorithm>…
题链: http://www.spoj.com/problems/NSUBSTR/ 题解: 同届红太阳 --WSY给出的后缀数组解法!!! 首先用倍增算法求出 sa[i],rak[i],hei[i]然后维护出 L[i]数组表示:在后缀数组中,排名最小(记其排名为 L[i])的后缀与排名i的后缀的LCP>=hei[i]同理,R[i]数组表示:在后缀数组中,排名最大(记其排名为 R[i])的后缀与排名i的后缀的LCP>=hei[i]以上两个数组可以由单调栈 O(N)维护出来. 然后呢,令 ANS[…
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 <= 50000 Output For each test case output one number saying the number of disti…
题目传送门 传送门I 传送门II 题目大意 给定一个字符串,多次询问它的第$k$大本质不同的子串,输出它. 考虑后缀Trie.依次考虑每个后缀新增的本质不同的子串个数,显然,它是$n - sa[i] - height[i]$. 求出$height$数组后,求一求本质不同的子串个数的前缀和,可以对每个询问二分. 这里可以直接离线,$O(n + m)$扫一扫就好了. Code /** * SPOJ * Problem#SUBLEX * Accepted * Time: 30ms * Memory:…
DISUBSTR - Distinct Substrings 链接 题意: 询问有多少不同的子串. 思路: 后缀数组或者SAM. 首先求出后缀数组,然后从对于一个后缀,它有n-sa[i]-1个前缀,其中有height[rnk[i]]个被rnk[i]-1的后缀算了.所以再减去height[rnk[i]]即可. 代码: 换了板子. #include<cstdio> #include<algorithm> #include<cstring> #include<iostr…
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 distinc…
题目链接: http://acm.hust.edu.cn/vjudge/problem/113725 Repeated Substrings Time Limit: 3000MS 样例 sample input 3 aabaab aaaaa AaAaA sample output 5 4 5 题意 求出现过两次以上的不同子串有多少种. 题解 用后缀数组求出height[]数组,然后扫一遍,发现height[i]-height[i-1]>=0;就ans+=height[i]-height[i-1]…
\(\color{#0066ff}{ 题目描述 }\) 给定一个字符串,求该字符串含有的本质不同的子串数量. \(\color{#0066ff}{输入格式}\) T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 50000 \(\color{#0066ff}{输出格式}\) For each test case output one number saying th…
题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12240   Accepted: 4144 Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤i+k-1≤|T|. Given two stri…
题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15122   Accepted: 5309 Description You are given a number of case-sensitive strings of alphabetic characters, find the largest string X,…
题意: 给定一个串\(s\),\(s\)必有一个最大循环节的连续子串\(ss\),问最大循环次数是多少 思路: 我们可以知道,如果一个长度为\(L\)的子串连续出现了两次及以上,那么必然会存在\(s[0].s[L].s[2L] \cdots s[L * k]\)中至少有两个连续的位置是相同的,然后看字母\(s[L * i]和s[L * (i + 1)]\)往前往后最多能匹配多远,记住总长度\(len\),那么最大循环次数为\((len / L) + 1\). 参考: SPOJ 687. Repe…