spoj SUBST1 - New Distinct Substrings【SAM||SA】
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=cur,dis[cur=++con]=id;
int p=la;
for(;p&&!ch[p][c];p=fa[p])
ch[p][c]=cur;
if(!p)
fa[cur]=1;
else
{
int q=ch[p][c];
if(dis[q]==dis[p]+1)
fa[cur]=q;
else
{
int nq=++con;
dis[nq]=dis[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];
fa[q]=fa[cur]=nq;
for(;ch[p][c]==q;p=fa[p])
ch[p][c]=nq;
}
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
ans=0,con=1,cur=1;
memset(fa,0,sizeof(fa));
memset(ch,0,sizeof(ch));
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++)
ins(s[i]-'a',i);
for(int i=1;i<=con;i++)
ans+=dis[i]-dis[fa[i]];
printf("%lld\n",ans);
}
return 0;
}
SA是对排序相邻的两个后缀减掉相同部分(height)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=100005;
int T,n,wa[N],wb[N],wsu[N],wv[N],sa[N],rk[N],he[N];
char s[N];
long long ans;
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
bool cmp(int r[],int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void saa(char r[],int n,int m)
{
int *x=wa,*y=wb;
for(int i=0;i<=m;i++)
wsu[i]=0;
for(int i=1;i<=n;i++)
wsu[x[i]=r[i]]++;
for(int i=1;i<=m;i++)
wsu[i]+=wsu[i-1];
for(int i=n;i>=1;i--)
sa[wsu[x[i]]--]=i;
for(int j=1,p=1;j<n&&p<n;j<<=1,m=p)
{
p=0;
for(int i=n-j+1;i<=n;i++)
y[++p]=i;
for(int i=1;i<=n;i++)
if(sa[i]>j)
y[++p]=sa[i]-j;
for(int i=1;i<=n;i++)
wv[i]=x[y[i]];
for(int i=0;i<=m;i++)
wsu[i]=0;
for(int i=1;i<=n;i++)
wsu[wv[i]]++;
for(int i=1;i<=m;i++)
wsu[i]+=wsu[i-1];
for(int i=n;i>=1;i--)
sa[wsu[wv[i]]--]=y[i];
swap(x,y);
p=1;
x[sa[1]]=1;
for(int i=2;i<=n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p:++p;
}
for(int i=1;i<=n;i++)
rk[sa[i]]=i;
for(int i=1,j,k=0;i<=n;he[rk[i++]]=k)
for(k?k--:0,j=sa[rk[i]-1];r[i+k]==r[j+k];k++);
}
int main()
{
scanf("%d",&T);
while(T--)
{
ans=0;
scanf("%s",s+1);
n=strlen(s+1);
saa(s,n,200);
for(int i=1;i<=n;i++)
ans+=n-sa[i]+1-he[i];
printf("%lld\n",ans);
}
return 0;
}
spoj SUBST1 - New Distinct Substrings【SAM||SA】的更多相关文章
- SPOJ - SUBST1 New Distinct Substrings —— 后缀数组 单个字符串的子串个数
题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 Given a ...
- 后缀数组:SPOJ SUBST1 - New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ Distinct Substrings【后缀数组】
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- Spoj SUBST1 New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ SUBST1 New Distinct Substrings(后缀数组 本质不同子串个数)题解
题意: 问给定串有多少本质不同的子串? 思路: 子串必是某一后缀的前缀,假如是某一后缀\(sa[k]\),那么会有\(n - sa[k] + 1\)个前缀,但是其中有\(height[k]\)个和上一 ...
- 【刷题】SPOJ 705 SUBST1 - New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))
Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...
- SPOJ 694&&SPOJ705: Distinct Substrings
DISUBSTR - Distinct Substrings 链接 题意: 询问有多少不同的子串. 思路: 后缀数组或者SAM. 首先求出后缀数组,然后从对于一个后缀,它有n-sa[i]-1个前缀,其 ...
- 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 ...
随机推荐
- 【C#】高级语言特有的单例模式
public class Singleton { private Singleton () { } // 变量标记为 readonly.第一次引用类的成员或创建实例时,仅仅实例化一次instance对 ...
- Printing multipage output
Printing known-length multipage output Using the PrintDataGrid control for multipage grids Example: ...
- SAE云平台的使用
参考文章:http://www.cnblogs.com/luyangsblog/p/3956135.html Web开发从零单排之一:在新浪云平台SAE上开发一个htm ...
- ShopMall
https://github.com/KingsleyYau/ShopMall-Android https://github.com/KingsleyYau/ShopMall-iOS https:// ...
- 命令行添加PATH
如何设置PATH 命令:echo "export PATH=xxxxxx:$PATH" >> ~/.bash_profile 解释:把"export PATH ...
- Creating a .bash_profile on your mac
A typical install of OS X won't create a .bash_profile for you. When you want to run functions from ...
- Spark学习笔记:(一)入门 glance
参考: http://spark.apache.org/docs/latest/quick-start.html 其它资料: http://mojijs.com/2015/04/190845/i ...
- 按行读入xml文件,删除不需要的行 -Java
删除挺麻烦的,这里其实只是把需要的行存到arraylist中再存到另一个文件中 import java.io.BufferedReader;import java.io.BufferedWriter; ...
- Dom解析XMl文档
XMl文档 <?xml version = "1.0" encoding = "UTF-8"?> <books> <book bo ...
- centos 网络连接查看
安装iftop. 这个得先安装epel #yum install epel-replease -y #yum install iftop -y #iftop