最少出现K次我们可以用Height数组的lcp来得出,而恰好出现K次,我们只要除去最少出现K+1次的lcp即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std; const int maxn = + ; int t1[maxn], t2[maxn], c[maxn]; bool cmp(int* r, int a, int b, int l) {
return r[a] == r[b] && r[a + l] == r[b + l];
} void da(int str[], int sa[], int Rank[], int lcp[], int n, int m) {
++n;
int i, j, p, *x = t1, *y = t2;
for (i = ; i < m; ++i) c[i] = ;
// puts("hha");
for (i = ; i < n; ++i) c[x[i] = str[i]]++;
for (i = ; i < m; ++i) c[i] += c[i - ];
for (i = n - ; i >= ; --i) sa[--c[x[i]]] = i;
for (j = ; j <= n; j <<= ) {
p = ;
for (i = n - j; i < n; ++i) y[p++] = i;
for (i = ; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
for (i = ; i < m; ++i) c[i] = ;
for (i = ; i < n; ++i) c[x[y[i]]]++; for (i = ; i < m; ++i) c[i] += c[i - ];
for (i = n - ; i >= ; --i) sa[--c[x[y[i]]]] = y[i]; swap(x, y);
p = ; x[sa[]] = ;
for (i = ; i < n; ++i) {
x[sa[i]] = cmp(y, sa[i - ], sa[i], j) ? p - : p++;
}
if (p >= n)break;
m = p;
} int k = ;
n--;
for (i = ; i <= n; ++i) Rank[sa[i]] = i;
for (i = ; i < n; ++i) {
if (k)--k;
j = sa[Rank[i] - ];
while (str[i + k] == str[j + k])++k;
lcp[Rank[i]] = k;
//cout << k << endl;
}
} int lcp[maxn], a[maxn], sa[maxn], Rank[maxn]; char s[maxn]; int d[maxn][];
int len; void rmq_init(int* A, int n) {
for (int i = ; i < n; ++i) d[i][] = A[i];
for (int j = ; ( << j) <= n; ++j)
for (int i = ; i + ( << j) - < n; ++i)
d[i][j] = min(d[i][j - ], d[i + ( << (j - ))][j - ]);
} int ASK(int l, int r) {
int k = ;
while (( << (k + )) <= r - l + )++k;
return min(d[l][k], d[r - ( << k) + ][k]);
} int ask(int l, int r) {
if (l == r) return len - sa[r]; /// l == r的话 是一个串, 返回本身的长度即可。
return ASK(l + , r); ///否则在rmq查询。
} //
int main() {
//freopen("in.txt", "r", stdin);
//freopen("outstd.txt", "w", stdout);
int T;
scanf("%d", &T);
while (T--) {
int k;
scanf("%d", &k);
scanf("%s", s);
//puts(s);
len = strlen(s);
for (int i = ; i < len; ++i) {
a[i] = s[i] - 'a' + ;
}
a[len] = ;
da(a, sa, Rank, lcp, len, );
rmq_init(lcp, len + );
long long ans = ;
if (k == )
{
for (int i = ; i <= len; i++)
{
int siz = len - sa[i];
int des = ;
if (i > ) des = max(des, ask(i - , i));
if (i < len) des = max(des, ask(i, i + ));
ans += siz - des;
}
}
else
{
for (int i = ; i+k- <= len; i++)
{
int siz = ask(i, i + k - );
int des = ;
if (i > ) des = max(des, ask(i - , i + k - ));
if (i + k <= len) des = max(des, ask(i, i + k));
ans += siz - des;
}
}
printf("%I64d\n", ans);
/*
long long ans = 0; for (int i = 1; i + k - 1 <= len; ++i) {
ans += ask(i, i + k - 1);
if (i - 1 > 0)ans -= ask(i - 1, i + k - 1); ///注意边界问题。
if (i + k <= len)ans -= ask(i, i + k);
if (i - 1 > 0 && i + k <= len)ans += ask(i - 1, i + k);
}
printf("%I64d\n", ans);*/ }
return ;
}

hdu-6194 string string string 后缀数组 出现恰好K次的串的数量的更多相关文章

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

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

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

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

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

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

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

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

  5. hdu 4691 Front compression (后缀数组)

    hdu 4691 Front compression 题意:很简单的,就是给一个字符串,然后给出n个区间,输出两个ans,一个是所有区间的长度和,另一个是区间i跟区间i-1的最长公共前缀的长度的数值的 ...

  6. HDU 3518 Boring counting(后缀数组,字符处理)

    题目 参考自:http://blog.sina.com.cn/s/blog_64675f540100k9el.html 题目描述: 找出一个字符串中至少重复出现两次的字串的个数(重复出现时不能重叠). ...

  7. HDU 4691 Front compression(后缀数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4691 题意:给出Input,求出Compressed output.输出各用多少字节. 思路:求后缀数 ...

  8. hdu 1403 Longest Common Substring 后缀数组 模板题

    题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...

  9. HDU - 4552 怪盗基德的挑战书 (后缀数组)

    Description "在树最漂亮的那天,当时间老人再次把大钟平均分开时,我会降临在灯火之城的金字塔前.带走那最珍贵的笑容."这是怪盗基德盗取巴黎卢浮宫的<蒙娜丽莎的微笑& ...

随机推荐

  1. bzoj 1251: 序列终结者 平衡树,fhqtreap

    链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1251 思路 好简单的模板题 不过还是wrong了好几发 叶子节点要注意下,不能使用 遇到就不 ...

  2. Dubbo集群配置和官方文档

    集群配置: https://blog.csdn.net/zh520qx/article/details/63679908 https://www.cnblogs.com/hd3013779515/p/ ...

  3. 【第四十章】Spring Boot 自定义拦截器

    1.首先编写拦截器代码 package com.sarnath.interceptor; import javax.servlet.http.HttpServletRequest; import ja ...

  4. fastqc

    fastqc用于查看测序数据的质量. 1.下载: http://www.bioinformatics.babraham.ac.uk/projects/download.html#fastqc wget ...

  5. Python有趣现象(不定时更新)

    1.list中extend方法有趣现象 1.1 List+=Str 与 List.extend(Str) list1 = [11,2,45] str1 = 'Michael' list1.extend ...

  6. printf和std::cout ...endl

    printf效率要比std::cout...endl高些,可以减少打印所花时间

  7. Sublime Text3 插件:DocBlockr与javascript注释规范

    原:http://www.ithao123.cn/content-719950.html 1.引子 在写代码的时候,尤其是写脚本,最需要注释了.目前脚本.样式的注释格式都有一个已经成文的约定规范(这些 ...

  8. python实现八皇后问题

    import random def judge(state, nextX): #判断是否和之前的皇后状态有冲突 nextY = len(state) for i in range(nextY): if ...

  9. 前端面试题 | JS部分(附带答案)

    目前在找工作,所以各方收集了一堆面试题.其实刷面试题的过程也能更新自己对知识的认识,所以也提醒自己多看多理解.如果对下面题目有更深理解,会实时更新.遇到新题目,也会不定时更新.希望能帮助到部分朋友- ...

  10. 用 Anaconda 完美解决 Python2 和 python3 共存问题

    Python3 被越来越多的开发者所接受,同时让人尴尬的是很多遗留的老系统依旧运行在 Python2 的环境中,因此有时你不得不同时在两个版本中进行开发,调试. 如何在系统中同时共存 Python2 ...