【SPOJ】Distinct Substrings(后缀自动机)
2018-02-13 11:34
169 查看
【SPOJ】Distinct Substrings(后缀自动机)
题面
Vjudge题意:求一个串的不同子串的数量
题解
对于这个串构建后缀自动机之后我们知道每个串出现的次数就是\(right/endpos\)集合的大小
但是实际上我们没有任何必要减去不合法的数量
我们只需要累加每个节点表示的合法子串的数量即可
这个值等于\(longest-shortest+1=longest-parent.longest\)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<set> #include<map> #include<vector> #include<queue> using namespace std; #define ll long long #define RG register #define MAX 1000100 inline int read() { RG int x=0,t=1;RG char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-')ch=getchar(); if(ch=='-')t=-1,ch=getchar(); while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar(); return x*t; } char ch[MAX]; ll ans; int tot=1,last=1; struct Node { int son[26]; int ff,len; }t[MAX<<1]; void extend(int c) { int p=last,np=++tot;last=np; t[np].len=t[p].len+1; while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff; if(!p)t[np].ff=1; else { int q=t[p].son[c]; if(t[p].len+1==t[q].len)t[np].ff=q; else { int nq=++tot; t[nq]=t[q];t[nq].len=t[p].len+1; t[q].ff=t[np].ff=nq; while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff; } } } int main() { int T=read(); while(T--) { scanf("%s",ch+1); ans=0;last=tot=1;memset(t,0,sizeof(t)); for(int i=1,l=strlen(ch+1);i<=l;++i)extend(ch[i]-65); for(int i=1;i<=tot;++i)ans+=t[i].len-t[t[i].ff].len; printf("%lld\n",ans); } return 0; }
相关文章推荐
- 【后缀自动机】[SPOJ LCS]Longest Common Substring
- 【BZOJ 2780】[Spoj]8093 Sevenk Love Oimaster 广义后缀自动机
- SPOJ 题目1812 LCS2 - Longest Common Substring II(后缀自动机求多个串的最长公共子串)
- 【bzoj2780】[Spoj]8093 Sevenk Love Oimaster 广义后缀自动机
- SPOJ 1812 Longest Common Substring II 后缀自动机求多字符串最长公共子串
- SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)
- SPOJ 1812 LCS2 后缀自动机
- SPOJ 1812 Longest Common Substring II(后缀自动机)
- 【SPOJ】Longest Common Substring II (后缀自动机)
- 【SPOJ】8222. Substrings(后缀自动机)
- SPOJ LCS Longest Common Substring 后缀自动机
- Spoj REPEATS 后缀自动机+set
- SPOJ 题目7528 SUBLEX - Lexicographical Substring Search(后缀自动机求排名k的子串)
- SPOJ 1811 LCS 后缀自动机
- 【后缀自动机】[SPOJLCS]Longest Common Substring
- SPOJ-1811 Longest Common Substring(后缀自动机)
- spoj 1811 LCS 后缀自动机
- spoj1812-Longest Common Substring II(后缀自动机)
- 【SPOJ】Longest Common Substring II (后缀自动机)
- 后缀自动机(最长公共子串、模板)spoj1811