描述
现在有一个字符串,请求出这个字符串不相同的子串个数。
YXY现在不会做,请你来帮忙……

n<=20W

题解:

后缀数组裸题,其实我在练习模板写对了没

代码:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 200000+5
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,c[maxn],sa[maxn],rk[maxn],h[maxn],t[maxn],t2[maxn];
char s[maxn];
void build(int m)
{
int *x=t,*y=t2;
for0(i,m)c[i]=;
for0(i,n)c[x[i]=s[i]]++;
for1(i,m)c[i]+=c[i-];
for3(i,n,)sa[--c[x[i]]]=i;
for(int k=;k<=n+;k<<=)
{
int p=;
for2(i,n-k+,n)y[p++]=i;
for0(i,n)if(sa[i]>=k)y[p++]=sa[i]-k;
for0(i,m)c[i]=;
for0(i,n)c[x[y[i]]]++;
for1(i,m)c[i]+=c[i-];
for3(i,n,)sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=;x[sa[]]=;
for1(i,n)x[sa[i]]=y[sa[i-]]==y[sa[i]]&&y[sa[i-]+k]==y[sa[i]+k]?p-:p++;
if(p>=n+)break;
m=p-;
}
}
void calch()
{
for1(i,n)rk[sa[i]]=i;
for(int i=,k=,j;i<n;h[rk[i++]]=k)
for(k?k--:,j=sa[rk[i]-];s[i+k]==s[j+k];k++);
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();int len=;
while(len<n)
{
scanf("%s",s+len);
len=strlen(s);
}
s[n]=' ';
build();
calch();
ll ans=;
for1(i,n)ans+=(ll)(n-sa[i]-h[i]);
cout<<ans<<endl;
return ;
}

这次的模板比上次好多了。。。

vijosP1567子串计数的更多相关文章

  1. 【暴力Treap 或 离线归并】子串计数(genies)

    子串计数(genies) Description 给出一段含有n个元素的序列a,要求求出子串和小于等于t的子串个数 Input Data 输入共两行第一行包含两个整数,n,t分别表示序列a元素的个数和 ...

  2. 【poj3415-Common Substrings】sam子串计数

    题意:  给出两个串,问这两个串的所有的子串中(重复出现的,只要是位置不同就算两个子串),长度大于等于k的公共子串有多少个. 题解: 这题好像大神们都用后缀数组做..然而我在sam的题表上看到这题,做 ...

  3. [bzoj3277==bzoj3473]出现k次子串计数——广义后缀自动机+STL

    Brief Description 给定n个字符串,对于每个字符串,您需要求出在所有字符串中出现次数大于等于k次的子串个数. Algorithm Design 先建立一个广义后缀自动机,什么是广义后缀 ...

  4. HDU 5806 NanoApe Loves Sequence Ⅱ (模拟)

    NanoApe Loves Sequence Ⅱ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5806 Description NanoApe, t ...

  5. 后缀自动机(SAM)

    *在学习后缀自动机之前需要熟练掌握WA自动机.RE自动机与TLE自动机* 什么是后缀自动机 后缀自动机 Suffix Automaton (SAM) 是一个用 O(n) 的复杂度构造,能够接受一个字符 ...

  6. 字符串数据结构模板/题单(后缀数组,后缀自动机,LCP,后缀平衡树,回文自动机)

    模板 后缀数组 #include<bits/stdc++.h> #define R register int using namespace std; const int N=1e6+9; ...

  7. 笔试算法题(30):从已排序数组中确定数字出现的次数 & 最大公共子串和最大公共序列(LCS)

    出题:在已经排序的数组中,找出给定数字出现的次数: 分析: 解法1:由于数组已经排序,所以可以考虑使用二分查找确定给定数字A的第一个出现的位置m和最后一个出现的位置n,最后m-n+1就是A出现的次数: ...

  8. 后缀自己主动机(SAM)学习指南

    *在学习后缀自己主动机之前须要熟练掌握WA自己主动机.RE自己主动机与TLE自己主动机* 什么是后缀自己主动机 后缀自己主动机 Suffix Automaton (SAM) 是一个用 O(n) 的复杂 ...

  9. 【字符串】后缀数组SA

    后缀数组 概念 实际上就是将一个字符串的所有后缀按照字典序排序 得到了两个数组 \(sa[i]\) 和 \(rk[i]\),其中 \(sa[i]\) 表示排名为 i 的后缀,\(rk[i]\) 表示后 ...

随机推荐

  1. SQL 刪除

    SQL 刪除 1.delete from table_name 2.drop table table_name drop table is different from deleting all of ...

  2. Python快速入门学习笔记(二)

    注:本学习笔记参考了廖雪峰老师的Python学习教程,教程地址为:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb49318210 ...

  3. cannot convert from '_TCHAR *' to 'char *'

    Reference: Why can't convert TCHAR* to char* Need to Use Multi-Byte Character Set in project's setti ...

  4. Arithmetic Expression

    时间限制:2000ms 单点时限:200ms 内存限制:256MB 描述 Given N arithmetic expressions, can you tell whose result is cl ...

  5. Poj 1013 Counterfeit Dollar / OpenJudge 1013(2692) 假币问题

    1.链接地址: http://poj.org/problem?id=1013 http://bailian.openjudge.cn/practice/2692 http://bailian.open ...

  6. oracle expdp 无法导出SYS下特定TABLE

    创建测试表: D:\app\product\\db_1>sqlplus "/as sysdba" SQL :: Copyright (c) , , Oracle. All r ...

  7. 图论(四)------非负权有向图的单源最短路径问题,Dijkstra算法

    Dijkstra算法解决了有向图G=(V,E)上带权的单源最短路径问题,但要求所有边的权值非负. Dijkstra算法是贪婪算法的一个很好的例子.设置一顶点集合S,从源点s到集合中的顶点的最终最短路径 ...

  8. chrome extension/plugin path

    C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default\Extensions

  9. 开发软件设计模型 visual studio UML

    http://www.ibm.com/developerworks/cn/rational/rationaledge/content/feb05/bell/ http://msdn.microsoft ...

  10. mongodb 全文检索

    MongoDB 全文检索 全文检索对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索引进行查找,并将查找的结果反馈给用户的检索方式. 这个过程类似于通过 ...