luoguP2408不同子串个数
传送门
解法一:后缀数组
可以知道每一个子串都是后缀的前缀,那么对于第\(i\)小的后缀的贡献就可以表示为n-sa[i]+1
然而会存在重复的子串,注意height数组的定义,对于sa[i-1]和sa[i],只有height[i]个子串会被重复计算,每次都减掉就好了
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=1e5+10;long long ans;
int n,a[maxn],m='z',x[maxn],y[maxn],num,sa[maxn],rk[maxn],h[maxn];char p[maxn];
int main()
{
read(n);scanf("%s",p+1);
for(rg int i=1;i<=n;i++)a[x[i]=p[i]]++;
for(rg int i=1;i<=m;i++)a[i]+=a[i-1];
for(rg int i=n;i;i--)sa[a[x[i]]--]=i;
for(rg int k=1;k<=n;k<<=1,num=0)
{
for(rg int i=n-k+1;i<=n;i++)y[++num]=i;
for(rg int i=1;i<=n;i++)if(sa[i]>k)y[++num]=sa[i]-k;
for(rg int i=1;i<=m;i++)a[i]=0;
for(rg int i=1;i<=n;i++)a[x[i]]++;
for(rg int i=1;i<=m;i++)a[i]+=a[i-1];
for(rg int i=n;i;i--)sa[a[x[y[i]]]--]=y[i];
for(rg int i=1;i<=n;i++)y[i]=x[i];
num=x[sa[1]]=1;
for(rg int i=2;i<=n;i++)
if(y[sa[i]]!=y[sa[i-1]]||y[sa[i]+k]!=y[sa[i-1]+k])x[sa[i]]=++num;
else x[sa[i]]=num;
if(num>=n)break;m=num;
}
for(rg int i=1;i<=n;i++)rk[sa[i]]=i;
for(rg int i=1,k=0,j;i<=n;h[rk[i++]]=k)
for(k=k?k-1:k,j=sa[rk[i]-1];p[j+k]==p[i+k];k++);
for(rg int i=1;i<=n;i++)ans+=n-sa[i]-h[i]+1;printf("%lld\n",ans);
}
解法二:后缀自动机
建出后缀自动机,拓扑排序就行了
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
void read(int &x){
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=5e6+10;
int n,tot,las,pre[maxn],nxt[maxn],h[maxn],cnt,in[maxn];
char a[maxn];
long long ans,f[maxn];
struct sam{int len,link,ch[26];}s[maxn];
void sam_pre(){s[0].len=0,s[0].link=-1;}
void ins(int x){
int cur=++tot,p=las;s[cur].len=s[p].len+1;
while(p!=-1&&!s[p].ch[x])s[p].ch[x]=cur,p=s[p].link;
if(p==-1)s[cur].link=0;
else{
int q=s[p].ch[x];
if(s[q].len==s[p].len+1)s[cur].link=q;
else{
int now=++tot;s[now].len=s[p].len+1;
s[now].link=s[q].link;
memcpy(s[now].ch,s[q].ch,sizeof s[q].ch);
while(p!=-1&&s[p].ch[x]==q)s[p].ch[x]=now,p=s[p].link;
s[q].link=s[cur].link=now;
}
}
las=cur;
}
void add(int x,int y){pre[++cnt]=y,nxt[cnt]=h[x],h[x]=cnt;}
void top_sort(){
queue<int>q;
for(rg int i=0;i<=tot;i++)if(!in[i])q.push(i);
while(!q.empty()){
int x=q.front();q.pop();
ans+=f[x];
for(rg int i=h[x];i;i=nxt[i]){
f[pre[i]]+=f[x];
if(!(--in[pre[i]]))q.push(pre[i]);
}
}
}
int main(){
read(n),scanf("%s",a+1),sam_pre();
for(rg int i=1;i<=n;i++)ins(a[i]-'a');
for(rg int i=0;i<=tot;i++)
for(rg int j=0;j<26;j++)
if(s[i].ch[j])add(i,s[i].ch[j]),in[s[i].ch[j]]++;
f[0]=1,top_sort(),printf("%lld\n",ans-1);
}
luoguP2408不同子串个数的更多相关文章
- [TyvjP1515] 子串统计 [luoguP2408] 不同子串个数(后缀数组)
Tyvj传送门 luogu传送门 经典题 统计一个字符串中不同子串的个数 一个字符串中的所有子串就是所有后缀的前缀 先求出后缀数组,求出后缀数组中相邻两后缀的 lcp 那么按照后缀数组中的顺序遍历求解 ...
- HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- HDU 3948 不同回文子串个数
集训队论文中有求不同子串个数的做法,就是扫一遍height数组,过程中根据height数组进行去重.对于本题也是雷同的,只是每一次不是根据与排名在上一位的LCP去重,而是与上一次统计对答案有贡献的后缀 ...
- HDU4622 (查询一段字符串的不同子串个数,后缀自动机)
http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给出一个字符串和q次询问,每次询问[l,r]区间内不同子串的个数 分析: N<=2000. 我 ...
- [spoj DISUBSTR]后缀数组统计不同子串个数
题目链接:https://vjudge.net/contest/70655#problem/C 后缀数组的又一神奇应用.不同子串的个数,实际上就是所有后缀的不同前缀的个数. 考虑所有的后缀按照rank ...
- ACdream 1430——SETI——————【后缀数组,不重叠重复子串个数】
SETI Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statist ...
- HDU 5056 Boring count(不超过k个字符的子串个数)
Boring count Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Luogu P2408 不同子串个数【SAM】
P2408 不同子串个数 计算一个字符串的不同子串个数 两种方法,一种是\(dp\)出来\(SAM\)从起点开始的路径数量 另一种方法就是计算每个点的\(len[i]-len[link[i]]\)这个 ...
- hdu5056(找相同字母不出现k次的子串个数)
题意: 给你一个字符串,然后问你这个字符串里面有多少个满足要求的子串,要求是每个子串相同字母出现的次数不能超过k. 思路: 这种题目做着比较有意思,而且不是很难(但自己还是嘚瑟,w ...
随机推荐
- exp/imp三种模式——完全、用户、表
ORACLE数据库有两类备份方法.第一类为物理备份,该方法实现数据库的完整恢复,但数据库必须运行在归挡模式下(业务数据库在非归挡模式下运行),且需要极大的外部存储设备,例如磁带库:第二类备份方式为逻辑 ...
- SpringBoot-(5)-properties的使用
项目中经常需要进行一些配置,一般会使用springboot默认的application.properties文件,也可以自己创建配置文件 一,application.properties配置 logg ...
- zookeeper+dubbo【转载】
转载地址:http://ahua186186.iteye.com/blog/1912421 注:zookeeper集群是myid文件是没有后缀名的. 转自: http://www.verydemo.c ...
- Linux常用命令之scp
目录 1.拷贝远程文件到本地 2.拷贝远程文件夹到本地 3.拷贝本地文件到远程 4.拷贝本地文件夹到远程 1.拷贝远程文件到本地 scp root@101.132.169.194:/home/wwwr ...
- BZOJ 1617 [Usaco2008 Mar]River Crossing渡河问题:dp
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1617 题意: Farmer John以及他的N(1 <= N <= 2,500 ...
- PIL 安装及使用
我ubunto虚拟机自带的是python2.7,好像PIL也只支持到2.7. PIL包的安装 Debian/Ubunto Linux下直接安装: sudo apt-get install python ...
- python中元组tuple
python中列表(list)和元组(tuple)有很多相似的地方,它们都是容器,由一系列的对象构成,都可以包含任意类型的元素,甚至是一个序列. list和tuple的不同首先体现在写法上: li ...
- PHP错题误区
1,$bool = TRUE;echo gettype($bool); //这个输出类型:booleanecho is_string($bool); //这个用echo是不能输出布尔型的,只有va ...
- B - Equidistant String
B - Equidistant String Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & % ...
- Eigen::aligned_allocator
http://blog.csdn.net/rs_huangzs/article/details/50574141