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

Problem Description

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.

 
Input

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.

 
Output
For each test case, print the number of the important substrings in a line.

 
Sample Input
2
2
abcabc
3
abcabcabcabc
 
Sample Output
6
9

题目链接: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)的更多相关文章

  1. 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 ...

  2. 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过

    题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&am ...

  3. POJ 3693 后缀数组+RMQ

    思路: 论文题 后缀数组&RMQ 有一些题解写得很繁 //By SiriusRen #include <cmath> #include <cstdio> #includ ...

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

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

  5. HDU5853 Jong Hyok and String(二分 + 后缀数组)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5853 Description Jong Hyok loves strings. One da ...

  6. CF1063F. String Journey(后缀数组+线段树)

    题目链接 https://codeforces.com/contest/1063/problem/F 题解 虽然本题有时间复杂度较高但非常好写的做法...... 首先,若答案为 \(k\),则一定存在 ...

  7. Codeforces 1063F - String Journey(后缀数组+线段树+dp)

    Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...

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

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

  9. HDU 4691 后缀数组+RMQ

    思路: 求一发后缀数组,求个LCP 就好了 注意数字有可能不只一位 (样例2) //By SiriusRen #include <bits/stdc++.h> using namespac ...

随机推荐

  1. 【cisco探索之路】

    CISCO探索之路 show&debug&clear 1:show show version:显示版本信息show running-config:显示当前的配置show interfa ...

  2. 仿制用友U8界面

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  3. ruby OpenURI模块使用

    OpenURI is an easy-to-use wrapper for Net::HTTP, Net::HTTPS and Net::FTP(OpenURI支持重定向) 像打开普通文件那样打开ht ...

  4. Leecode刷题之旅-C语言/python-88合并两个有序数组

    /* * @lc app=leetcode.cn id=88 lang=c * * [88] 合并两个有序数组 * * https://leetcode-cn.com/problems/merge-s ...

  5. GIT LFS 使用笔记

    一.背景 由于git上传文件大小受限,所以我们需要使用GIT LFS对大小超过一定上限的大文件进行处理. 二.安装 linux上安装参见 https://askubuntu.com/questions ...

  6. COURSES POJ1469(模板)

    Description Consider a group of N students and P courses. Each student visits zero, one or more than ...

  7. docker学习(三) 安装docker的web可视化管理工具

    1.docker是一个一款很轻便的应用容器引擎,为了更好的管理和使用docker,使用web可视化管理工具似乎更符合大多数人的需求.在这里,我给大家分享下自己使用过的几款web工具:docker UI ...

  8. 【转】moodle中年级、班级、小组研讨

    Moodle平台支持年级.班级.小组功能,提供了方便易用的分组工具.小组支持公开和封闭属性,配合教学功能模块,教师可以组织小组为单位的教学活动. 在Moodle中,年级.班级.小组主要是通过群组(co ...

  9. javascript 自定义发布与订阅

    //声明一个类,与普通的类的声明不一样, function Girl() { //将类的事件声明成一个私有的属性,里面是一个对象 this._events = {} } /* { "失恋&q ...

  10. 管理员常用Windows PowerShell命令Top25

    即使Windows PowerShell已经由来已久,但很多管理员并不愿意主动熟悉PowerShell cmdlet命令行.随着微软扩展了PowerShell的功能,管理员应该对其功能及使用烂熟于心. ...