HDU 6194 string string string(后缀数组+RMQ)
string string string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 608 Accepted Submission(s): 167
Uncle Mao is a wonderful ACMER. One day he met an easy problem, but Uncle Mao was so lazy that he left the problem to you. I hope you can give him a solution.
Given a string s, we define a substring that happens exactly k times as an important string, and you need to find out how many substrings which are important strings.
The first line contains an integer T (T≤100) implying the number of test cases.
For each test case, there are two lines:
the first line contains an integer k (k≥1) which is described above;
the second line contain a string s (length(s)≤105).
It's guaranteed that ∑length(s)≤2∗106.
∑length(s)≤2∗106.
题目链接:HDU 6194
比赛的时候过的人挺多,可惜刚学$SA$才几天,对$height$的理解还不够深刻,没写出来,只知道大概就是用后缀数组或者更加陌生的自动机什么做吧……
题解也是看别人的:传送门
大致做法就是求刚好出现$k$次的不好求,但是可以转换成至少出现$k$次减去至少出现$k+1$次来算,显然对于至少出现$k$次的问题就可以用$height$数组与RMQ来求,对长度为$k-1$的区间即$(i+1,i+k-1)$进行最小值查询,设最小值为$Min$,那么它对答案的贡献是$Min$,但是可能会多算,因此要减去出现$k+1$的次数,也就是把区间往左移动一个单位$(i,i+k-1)$,往右移动一个单位$(i,i+k)$,减去这两个的最小值$Min1$与$Min2$,此时又会多减掉一些贡献,这些多减掉的贡献就是长度为k的区间即$(i,i+k)$最小值Min3,要将其加回去。代码中用的是LCP函数,直接查询排名为$(l,r)$的区间,可以减少一些边界运算
代码:
#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
int wa[N], wb[N], sa[N], cnt[N];
char s[N];
int height[N], ran[N], dp[N][18];
int len; inline int cmp(int r[], int a, int b, int d)
{
return r[a] == r[b] && r[a + d] == r[b + d];
}
void DA(int n, int m)
{
int i;
int *x = wa, *y = wb;
for (i = 0; i < m; ++i)
cnt[i] = 0;
for (i = 0; i < n; ++i)
++cnt[x[i] = s[i]];
for (i = 1; i < m; ++i)
cnt[i] += cnt[i - 1];
for (i = n - 1; i >= 0; --i)
sa[--cnt[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 < m; ++i)
cnt[i] = 0;
for (i = 0; i < n; ++i)
++cnt[x[y[i]]];
for (i = 1; i < m; ++i)
cnt[i] += cnt[i - 1];
for (i = n - 1; i >= 0; --i)
sa[--cnt[x[y[i]]]] = y[i];
swap(x, y);
x[sa[0]] = 0;
p = 1;
for (i = 1; i < n; ++i)
x[sa[i]] = cmp(y, sa[i - 1], sa[i], k) ? p - 1 : p++;
m = p;
if (p >= n)
break;
}
}
void gethgt(int n)
{
int i, k = 0;
for (i = 1; i <= n; ++i)
ran[sa[i]] = i;
for (i = 0; i < n; ++i)
{
if (k)
--k;
int j = sa[ran[i] - 1];
while (s[j + k] == s[i + k])
++k;
height[ran[i]] = k;
}
}
namespace RMQ
{
void init(int l, int r)
{
int i, j;
for (i = l; i <= r; ++i)
dp[i][0] = height[i];
for (j = 1; l + (1 << j) - 1 <= r; ++j)
for (i = l; i + (1 << j) - 1 <= r; ++i)
dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
}
int query(int l, int r)
{
int len = r - l + 1;
int k = 0;
while (1 << (k + 1) <= len)
++k;
return min(dp[l][k], dp[r - (1 << k) + 1][k]);
}
LL LCP(int rankl, int rankr)
{
if(rankl > rankr)
swap(rankl, rankr);
if (rankl == rankr)
return (LL)len - sa[rankl];
else
return (LL)query(rankl + 1, rankr);
}
}
int main(void)
{
int T, k, i;
scanf("%d", &T);
while (T--)
{
scanf("%d", &k);
scanf("%s", s);
len = strlen(s);
DA(len + 1, 'z' + 1);
gethgt(len);
RMQ::init(1, len);
LL ans = 0;
for (i = 1; i + k - 1 <= len; ++i)
{
ans += RMQ::LCP(i, i + k - 1);
if (i - 1 >= 1)
ans -= RMQ::LCP(i - 1, i + k - 1);
if (i + k <= len)
ans -= RMQ::LCP(i, i + k);
if (i - 1 >= 1 && i + k <= len)
ans += RMQ::LCP(i - 1, i + k);
}
printf("%I64d\n", ans);
}
return 0;
}
HDU 6194 string string string(后缀数组+RMQ)的更多相关文章
- spoj687 REPEATS - Repeats (后缀数组+rmq)
A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed strin ...
- 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过
题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&am ...
- POJ 3693 后缀数组+RMQ
思路: 论文题 后缀数组&RMQ 有一些题解写得很繁 //By SiriusRen #include <cmath> #include <cstdio> #includ ...
- HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given ...
- HDU5853 Jong Hyok and String(二分 + 后缀数组)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5853 Description Jong Hyok loves strings. One da ...
- CF1063F. String Journey(后缀数组+线段树)
题目链接 https://codeforces.com/contest/1063/problem/F 题解 虽然本题有时间复杂度较高但非常好写的做法...... 首先,若答案为 \(k\),则一定存在 ...
- Codeforces 1063F - String Journey(后缀数组+线段树+dp)
Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...
- hdu 2459 (后缀数组+RMQ)
题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...
- HDU 4691 后缀数组+RMQ
思路: 求一发后缀数组,求个LCP 就好了 注意数字有可能不只一位 (样例2) //By SiriusRen #include <bits/stdc++.h> using namespac ...
随机推荐
- Linux系统磁盘管理
1 Linux磁盘管理体系简介 Linux磁盘管理分为五个步骤:首先在服务器上添加相应的硬盘(如/dev/sda.sdb.sdc等),对全新的服务器(即没有操作系统)做硬RAID0.RAID1.RAI ...
- js 关于字符串转数字及数字保留位数的控制
1.parseInt()和parseFloat()两个转换函数,将字符串转换成相应的数字. 1.parseInt() parseInt进行转换时,将字符串转成相应的整数.浮点数以后的数字都不要了. p ...
- Angular简单总结
AngularJS AngularJS四大特征 MVC模式 双向绑定 依赖注入 模块化设计 AngularJS 表达式 AngularJS 表达式写在双大括号内{{expression }},可以包含 ...
- Xshell4 出现Linux中中文字符乱码问题
Xshell5竟然收费了... 没办法只能用回Xshell4 但是不知道是版本不对还是在咋的 发现中文乱码,导致操作非常不方便 解决方案 LANG=zh_CN.big5 执行在终端执行上面的命令就可以 ...
- hdcms v5.7.0学习笔记
hdcms v5.7.0学习笔记 https://note.youdao.com/ynoteshare1/index.html?id=c404d63ac910eb15a440452f73d6a6db& ...
- Delphi 过程类型
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- 【转】在Ubuntu 16.10 Server 上部署 Moodle
第一步 安装 Ubuntu 16.10 Server LTS Moodle 的官方文档肯定了Ubuntu Server LTS 是适合运维Moodle平台的. 1.使用纯代码交互的服务器Ubuntu更 ...
- Lambda表达式详解【转】
前言 1.天真热,程序员活着不易,星期天,也要顶着火辣辣的太阳,总结这些东西. 2.夸夸lambda吧:简化了匿名委托的使用,让你让代码更加简洁,优雅.据说它是微软自c#1.0后新增的最重要的功能之一 ...
- LeetCode:11. ContainerWithWater(Medium)
原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an ...
- python语法join函数
Python语法中join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串. vid = )