UVALive - 6869 Repeated Substrings 后缀数组
题目链接:
http://acm.hust.edu.cn/vjudge/problem/113725
Repeated Substrings
Time Limit: 3000MS
样例
sample input
3
aabaab
aaaaa
AaAaAsample 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 后缀数组的更多相关文章
- UVALive 6869 Repeated Substrings
Repeated Substrings Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Descri ...
- CSU-1632 Repeated Substrings[后缀数组求重复出现的子串数目]
评测地址:https://cn.vjudge.net/problem/CSU-1632 Description 求字符串中所有出现至少2次的子串个数 Input 第一行为一整数T(T<=10)表 ...
- POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数
题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K ...
- POJ1226 Substrings ——后缀数组 or 暴力+strstr()函数 最长公共子串
题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS Memory Limit: 10000K Total ...
- SPOJ - SUBST1 New Distinct Substrings —— 后缀数组 单个字符串的子串个数
题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 Given a ...
- SPOJ- Distinct Substrings(后缀数组&后缀自动机)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ - DISUBSTR Distinct Substrings (后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- POJ 3415 Common Substrings 后缀数组+并查集
后缀数组,看到网上很多题解都是单调栈,这里提供一个不是单调栈的做法, 首先将两个串 连接起来求height 求完之后按height值从大往小合并. height值代表的是 sa[i]和sa[i ...
- POJ1226:Substrings(后缀数组)
Description You are given a number of case-sensitive strings of alphabetic characters, find the larg ...
随机推荐
- eclipse创建maven项目及Javaweb项目
1.开启eclipse,右键new——>other,如下图找到maven project 2.选择maven project,显示创建maven项目的窗口 3.在搜索框中搜索“web”,选择,n ...
- centos7关闭图形界面启动系统
手动敲那么多不累么?仅2条命令(好) 1,命令模式systemctl set-default multi-user.target 2,图形模式systemctl set-default graphic ...
- (mark)ubuntu16.04下安装&配置anaconda+tensorflow新手教程
https://blog.csdn.net/m0_37864814/article/details/82112029
- SQL 备忘录
都兼容 MySQL 查看表结构:DESC ${table_name} 查看建表语句:SHOW CREATE TABLE ${table_name} 表增加列:ALTER TABLE ${table_ ...
- 成都Uber优步司机奖励政策(4月9日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 成都Uber优步司机奖励政策(2月20日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 成都Uber优步司机奖励政策(1月22日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 广州Uber优步司机奖励政策(1月4日~1月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- crontab执行PHP
在stackoverflow上看到一个问题:http://stackoverflow.com/questions/14015543/crontab-php-wget-or-curl 有三种通过cron ...
- 「日常温习」Hungary算法解决二分图相关问题
前言 二分图的重点在于建模.以下的题目大家可以清晰的看出来这一点.代码相似度很高,但是思路基本上是各不相同. 题目 HDU 1179 Ollivanders: Makers of Fine Wands ...