Boring counting

\[Time Limit: 1000 ms \quad Memory Limit: 32768 kB
\]

题意

给出一个字符串,求出其中出现两次及以上的子串个数,要求子串之间不可以重合。

思路

对字符串后缀数组,然后枚举子串长度 \(len\),若某一段连续的 \(sa[i]\) 的 \(lcp \geq len\),那么说明这一段内存在一个长度为 \(lcp\) 的子串,而我们只需要其中的前 \(len\) 部分,接下来只要找出这个子串出现的最左和最右位置,然后判断中间能否放下这个长度为 \(len\) 的子串就可以了,如果可以的话,就让 \(ans\)++。


\(Hint\)

这题还让我学到后缀数组的另一个细节,在构建后缀数组之前,要在末尾加上一个比所有字符都小的字符,因为在求 \(height\) 的时候需要判断 \(a[i+k]==a[j+k]\),否则这里可能会无限扩展下去。

/***************************************************************
> File Name : a.cpp
> Author : Jiaaaaaaaqi
> Created Time : 2019年05月23日 星期四 22时40分17秒
***************************************************************/ #include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int> typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std; int n, m;
int cas, tol, T; char s[maxn];
int a[maxn], sa[maxn], tp[maxn], tax[maxn], rk[maxn], height[maxn]; void rsort(int n, int m) {
for(int i=0; i<=m; i++) tax[i] = 0;
for(int i=1; i<=n; i++) tax[rk[tp[i]]]++;
for(int i=1; i<=m; i++) tax[i] += tax[i-1];
for(int i=n; i>=1; i--) sa[tax[rk[tp[i]]]--] = tp[i];
} int cmp(int *f, int x, int y, int w) {
return f[x]==f[y] && f[x+w]==f[y+w];
} void SA(int *a, int n, int m) {
for(int i=1; i<=n; i++) rk[i] = a[i], tp[i] = i;
rsort(n, m);
for(int w=1, p=1, i; p<n; w<<=1, m=p) {
for(p=0, i=n-w+1; i<=n; i++) tp[++p] = i;
for(i=1; i<=n; i++) if(sa[i] > w) tp[++p] = sa[i]-w;
rsort(n, m), swap(rk, tp);
rk[sa[1]] = p = 1;
for(i=2; i<=n; i++) rk[sa[i]] = cmp(tp, sa[i], sa[i-1], w) ? p : ++p;
}
int j, k=0;
for(int i=1; i<=n; height[rk[i++]] = k)
for(k = k ? k-1 : k, j=sa[rk[i]-1]; a[i+k]==a[j+k]; k++);
} int calc(int len) {
int ans = 0;
int mx, mn;
mx = -inf, mn = inf;
for(int i=2; i<=n; i++) {
if(height[i]>=len) {
mx = max(mx, max(sa[i], sa[i-1]));
mn = min(mn, min(sa[i], sa[i-1]));
} else {
if(mx - mn >= len) ans++;
mx = -inf, mn = inf;
}
}
if(mx - mn >= len) ans++;
return ans;
} int main() {
while(scanf("%s", s+1)) {
if(s[1] == '#') break;
n = strlen(s+1);
s[++n] = 2;
for(int i=1; i<=n; i++) {
a[i] = s[i];
}
SA(a, n, 260);
// for(int i=1; i<=n; i++) {
// printf("sa[%d] = %d\n", i, sa[i]);
// }
int ans = 0;
for(int i=1; i<=(n+1)/2; i++) {
ans += calc(i);
}
printf("%d\n", ans);
}
return 0;
}

Boring counting HDU - 3518 (后缀数组)的更多相关文章

  1. Boring counting HDU - 3518 后缀自动机

    题意: 对于给出的字符串S, 长度不超过1000, 求其中本质不同的子串的数量, 这些子串满足在字符串S中出现了至少不重合的2次 题解: 将串放入后缀自动机中然后求出每一个节点对应的子串为后缀的子串出 ...

  2. Boring counting HDU - 3518 (后缀自动机)

    Boring counting \[ Time Limit: 1000 ms \quad Memory Limit: 32768 kB \] 题意 给出一个字符串,求出其中出现两次及以上的子串个数,要 ...

  3. hdu 3518(后缀数组)

    题意:容易理解... 分析:这是我做的后缀数组第一题,做这个题只需要知道后缀数组中height数组代表的是什么就差不多会做了,height[i]表示排名第i的后缀与排名第i-1的后缀的最长公共前缀,然 ...

  4. hdu 3948 后缀数组

    The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (J ...

  5. HDU - 3948 后缀数组+Manacher

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3948 题意:给定一个字符串,求字符串本质不同的回文子串个数. 思路:主要参考该篇解题报告 先按照man ...

  6. HDU 5769 后缀数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5769 [2016多校contest-4] 题意:给定一个字符,还有一个字符串,问这个字符串存在多少个不 ...

  7. HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given ...

  8. hdu 2459 (后缀数组+RMQ)

    题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...

  9. hdu 5442 (后缀数组)

    稍微学习了下第一次用后缀数组- - , 强行凑出答案 , 感觉现在最大的问题是很多算法都不知道 ,导致有的题一点头绪都没有(就像本题).  /*推荐 <后缀数组——处理字符串的有力工具>— ...

随机推荐

  1. 13. Scala函数式编程(高级部分)

    13.1 偏函数(partial function) 13.1.1 需求 -> 思考 一个集合val list = List(1,2,3,4,"abc"),完成如下要求 1) ...

  2. LOJ2257 SNOI2017 遗失的答案 容斥、高维前缀和

    传送门 数字最小公倍数为\(L\)的充分条件是所有数都是\(L\)的约数,而\(10^8\)内最多约数的数的约数也只有\(768\)个.所以我们先暴力找到所有满足是\(L\)的约数.\(G\)的倍数的 ...

  3. k8s-Label(标签)

    k8s-Label(标签) 一.Label是什么? Label是Kubernetes系统中的一个核心概念.Label以key/value键值对的形式附加到各种对象上,如Pod.Service.RC.N ...

  4. Bootstrap:UI开发平台 sdk

    Bootstrap:UI开发平台 Bootstrap是一个易用.优雅.灵活.可扩展的前端工具包,里面包含了丰富的Web组件,包括布局.栅格.表格.表单.导航.按钮.进度条.媒体对象等,基于这些组件,可 ...

  5. ES7.3.0配置邮件告警

    情况说明: 三台es组成集群,一台kibana,版本均为7.3.0 es版本要求是白金版,基础版的不行,不过可以试用30天的白金版 步骤:先说我自己走通的流程,然后介绍官方说明 1.因为我这边使用的是 ...

  6. JAVAWEB之增删改查

    青年志愿者服务网(20分)   1.项目需求: 为了适应社会主义市场经济发展的需要,推动青年志愿服务体系和多层次社会保障体系的建立和完善,促进青年健康成长,石家庄铁道大学急需建设青年志愿者服务网,推进 ...

  7. docker 安装tomcat容器和mysql容器

    1. docker pull mysql:5.6 2.docker run -p 3306:3306 --name mysql -v /data/mysql/conf:/etc/mysql/conf. ...

  8. c#高效准确的条形码、线性条码、QR二维码读写类库-SharpBarcode介绍

    SharpBarcode是一款支持.NET(C#,VB)的高效易用的条形码.QR二维码的读取和生成类库. 主要功能: 1.支持几乎所有常见类型的线性条形码和QR二维码的读取,高效读取,准确率高. 2. ...

  9. [Linux] 树莓派编译python3.7.4

    python3.7.4 源码编译后遇到ssl错误: pip is configured with locations that require TLS/SSL, however the ssl mod ...

  10. 【转载】C#通过Copy方法快速复制DataTable对象

    C#中的Datatable数据变量的操作过程中,可以通过DataTable的Copy方法快速复制当前的DataTable变量到新对象中,复制数据包含当前DataTable的结构信息如列名,同时也包含当 ...