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 ...
随机推荐
- 关于 redux-saga 中 take 使用方法详解
本文介绍了关于redux-saga中take使用方法详解,分享给大家,具体如下: 带来一个自己研究好久的API使用方法. redux-saga中effect中take这个API使用方式,用的多的是ca ...
- weexapp 开发流程(一)开发环境配置
1.创建项目 weexpack create weexapp 2.安装必要插件 npm i jwt-simple vue-resource vue-router vuex vuex-router-sy ...
- CSS 的导入方式 (link or import ?)
前言 最常看见的CSS的使用方式有三种 1. 在span, div 等标签上直接使用 style 属性定义CSS <span style="color:blue">Th ...
- 关于Android滑动冲突的解决方法(二)
之前的一遍学习笔记主要就Android滑动冲突中,在不同方向的滑动所造成冲突进行了了解,这样的冲突非常easy理解,当然也非常easy解决.今天,就同方向的滑动所造成的冲突进行一下了解,这里就先以垂直 ...
- Spyder的汉化
我准备写下spyder的汉化问题:对于英文大佬,从来没得汉化问题,但是对于新手和英语差的来说,汉化还是有必要,至少用汉化过得软件能快速掌握软件等.后期会用软件了在慢慢习惯英文也不迟...哈哈哈哈.本文 ...
- linux led子系统(一)
就像学编程第一个范例helloworld一样,学嵌入式,单片机.fpga之类的第一个范例就是点亮一盏灯.对于庞大的linux系统,当然可以编写一个字符设备驱动来实现我们需要的led灯,也可以直接利用g ...
- Devices下设备的进程显示为问号的问题
adb shell getprop ro.debuggable 返回 ro.debuggable=0 说明这个机子的版本不是userdebug版本,是user版本 只有这 ...
- 3144: [Hnoi2013]切糕
3144: [Hnoi2013]切糕 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1526 Solved: 827[Submit][Status] ...
- 使用JavaScript获取浏览器Chrome版本信息
Navigator对象包含了有关浏览器的信息 可通过访问其属性appVersion或userAgent来获取浏览器Chrome版本 例如,我所使用的QQ浏览器的appVersion和userAgent ...
- 使用forever让node.js持久运行
何为forever?forever可以看做是一个nodejs的守护进程,能够启动,停止,重启我们的app应用. npm install forever -g #安装 forever start app ...