题目链接:

http://acm.hust.edu.cn/vjudge/problem/113725

Repeated Substrings

Time Limit: 3000MS

样例

sample input

3

aabaab

aaaaa

AaAaA

sample output

5

4

5

题意

求出现过两次以上的不同子串有多少种。

题解

用后缀数组求出height[]数组,然后扫一遍,发现height[i]-height[i-1]>=0;就ans+=height[i]-height[i-1]。

代码

#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++) typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8; //start---------------------------------------------------------------------- const int maxn=1e5+10; struct SuffixArray{
char s[maxn];
int sa[maxn],t[maxn],t2[maxn],c[maxn];
int n,m;
void init(int n,int m){
this->n=n;
this->m=m;
}
void build_sa(){
int i,*x=t,*y=t2;
for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[x[i]=s[i]]++;
for(i=1;i<m;i++) c[i]+=c[i-1];
for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
for(int k=1;k<=n;k<<=1){
int p=0;
for(i=n-k;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
// for(i=0;i<n;i++) y[p++]=(sa[i]-k+n)%n; for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[x[y[i]]]++;
for(i=1;i<m;i++) c[i]+=c[i-1];
for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; swap(x,y);
p=1; x[sa[0]]=0;
for(i=1;i<n;i++){
x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
}
if(p>=n) break;
m=p;
}
}
int rank[maxn],height[maxn];
void getHeight(){
int i,j,k=0;
for(i=0;i<n;i++) rank[sa[i]]=i;
for(i=0;i<n;i++){
if(k) k--;
if(rank[i]-1<0) continue;
int j=sa[rank[i]-1];
while(s[i+k]==s[j+k]) k++;
height[rank[i]]=k;
}
}
void solve(){
build_sa();
// for(int i=0;i<n;i++) printf("%s\n",s+sa[i]);
getHeight();
// for(int i=1;i<n;i++) printf("%d ",height[i]);
// puts("");
int ans=0;
for(int i=1;i<n;i++){
if(height[i]>height[i-1]) ans+=height[i]-height[i-1];
}
printf("%d\n",ans);
}
}mysa; char str[maxn]; int main() {
int tc,kase=0;
scanf("%d",&tc);
while(tc--){
scanf("%s",&str);
int n=strlen(str);
mysa.init(n+1,256);
strcpy(mysa.s,str);
mysa.solve();
}
return 0;
} //end-----------------------------------------------------------------------

UVALive - 6869 Repeated Substrings 后缀数组的更多相关文章

  1. UVALive 6869 Repeated Substrings

    Repeated Substrings Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Descri ...

  2. CSU-1632 Repeated Substrings[后缀数组求重复出现的子串数目]

    评测地址:https://cn.vjudge.net/problem/CSU-1632 Description 求字符串中所有出现至少2次的子串个数 Input 第一行为一整数T(T<=10)表 ...

  3. POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数

    题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K ...

  4. POJ1226 Substrings ——后缀数组 or 暴力+strstr()函数 最长公共子串

    题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS   Memory Limit: 10000K Total ...

  5. SPOJ - SUBST1 New Distinct Substrings —— 后缀数组 单个字符串的子串个数

    题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 Given a ...

  6. SPOJ- Distinct Substrings(后缀数组&后缀自动机)

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  7. SPOJ - DISUBSTR Distinct Substrings (后缀数组)

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  8. POJ 3415 Common Substrings 后缀数组+并查集

    后缀数组,看到网上很多题解都是单调栈,这里提供一个不是单调栈的做法, 首先将两个串 连接起来求height   求完之后按height值从大往小合并.  height值代表的是  sa[i]和sa[i ...

  9. POJ1226:Substrings(后缀数组)

    Description You are given a number of case-sensitive strings of alphabetic characters, find the larg ...

随机推荐

  1. Vuex的第一次接触

    前言:最近在做Vue实现去哪网,想要实现在城市列表页面,点击某个城市的时候,主页的头部的城市会随着改变,就是首页和城市页面有共用的数据要分享,这里使用Vuex 1. Vuex是什么? 是Vue官方推荐 ...

  2. 安装jenkins环境之jdk8

    Launchpad PPA Repositories是很有用的非ubuntu官方的第三方个人资源库,可以很方便地安装第三方软件. 但是在运行add-apt-repository命令时,有时会提示命令不 ...

  3. 类型“Observable<Response>”上不存在属性“map”

    出错提示: angular2 Property 'map' does not exist on type 'Observable<Response>' 类型“Observable<R ...

  4. Product Helper

    using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; /// <summary> /// 产品 ...

  5. Python学习:11.Python装饰器讲解(二)

    回顾 上一节我们进行了Python简单装饰器的讲解,但是python的装饰器还有一部分高级的使用方式,这一节就针对python装饰器高级部分进行讲解. 为一个函数添加多个装饰器 今天,老板又交给你一个 ...

  6. wordpress网站程序漏洞修复办法

    近日wordpress被爆出高危的网站漏洞,该漏洞可以伪造代码进行远程代码执行,获取管理员的session以及获取cookies值,漏洞的产生是在于wordpress默认开启的文章评论功能,该功能在对 ...

  7. 中国大学MOOC-C程序设计(浙大翁恺)—— 单词长度

    题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以‘.’结束.你要输出这行文本中每个单词的长度.这里的单词与语言无关,可以包括各种符号,比如“it's”算一个单词,长度为4.注意,行中 ...

  8. Active MQ C++实现通讯记录

    Active MQ  C++实现通讯 背景知识: ActiveMQ是一个易于使用的消息中间件. 消息中间件 我们简单的介绍一下消息中间件,对它有一个基本认识就好,消息中间件(MOM:Message O ...

  9. 第二节 双向链表的GO语言实现

    一.什么是双向链表 和单链表比较,双向链表的元素不但知道自己的下线,还知道自己的上线(越来越像传销组织了).小煤车开起来,图里面可以看出,每个车厢除了一个指向后面车厢的箭头外,还有一个指向前面车厢的箭 ...

  10. [Python3.X]python 实现斐波那契数列

    斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一 ...