最少出现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. C# 文件与二进制之间的转换

    /// <summary> /// 工具类:文件与二进制流间的转换 /// </summary> public class FileBinaryConvertHelper { ...

  2. shell编程中的单/双 小括号, 中括号, 大括号

    linux shell中的变量类型?分字符串或者数字或者bool类型吗? 参考: http://www.cnblogs.com/nufangrensheng/p/3477281.html 不分! sh ...

  3. Tutorial: Generate BBox or Rectangle to locate the target obejct

    Tutorial: Generate BBox or Rectangle to locate the target obejct clc;close all;clear all; Img=imread ...

  4. 用RAR将多个文件夹一次性压缩为多个对应zip文件

    选中要压缩的所有文件夹.右键,选“添加到压缩文件...”,弹出的菜单如下图: 点击菜单栏“文件”.在“把每个文件都单独压缩文件中”选中,才可以单独创建压缩.如下图

  5. LOJ 534 花团(线段树+dfs栈)

    题意 https://loj.ac/problem/534 思路 又是复杂度错误的一题,\(O(n^2\log n)\) 能过 \(15000\) . 虽然看起来强制在线,其实是一道假的在线题.首先按 ...

  6. Git误操作 git reset强制回滚 恢复commit方法

    参考: 找回Git中丢失的Commit Git误操作 git reset强制回滚 恢复commit方法 使用Git时,常有误操作,在Commit之后又执行了git reset --hard HEAD强 ...

  7. HDU 5782 Cycle(KMP+哈希)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5782 题意:给出两个长度相等的字符串,输出两个字符的每个前缀是否循环相同. 思路: 如果连个串循环相 ...

  8. Unity Shaderlab: Object Outlines 转

    转 https://willweissman.wordpress.com/tutorials/shaders/unity-shaderlab-object-outlines/ Unity Shader ...

  9. Mac系统配置JDK环境变量

    1.安装 因为并非所有用户都用得着 Java ,所以在默认状态下 OS X 不预装 Java , 如果你需要的话可以手动安装. 到 Oracle 下载最新版的 Java 8 JDK 安装,安装目录可通 ...

  10. MAC office2016 安装及激活

    Office 下载地址: http://www.xitongtiandi.net/soft_yy/4285.html 破解补丁下载地址: https://bbs.feng.com/tencentdow ...