hdu-6194 string string string 后缀数组 出现恰好K次的串的数量
最少出现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次的串的数量的更多相关文章
- 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 4691 Front compression (后缀数组)
hdu 4691 Front compression 题意:很简单的,就是给一个字符串,然后给出n个区间,输出两个ans,一个是所有区间的长度和,另一个是区间i跟区间i-1的最长公共前缀的长度的数值的 ...
- HDU 3518 Boring counting(后缀数组,字符处理)
题目 参考自:http://blog.sina.com.cn/s/blog_64675f540100k9el.html 题目描述: 找出一个字符串中至少重复出现两次的字串的个数(重复出现时不能重叠). ...
- HDU 4691 Front compression(后缀数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4691 题意:给出Input,求出Compressed output.输出各用多少字节. 思路:求后缀数 ...
- hdu 1403 Longest Common Substring 后缀数组 模板题
题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...
- HDU - 4552 怪盗基德的挑战书 (后缀数组)
Description "在树最漂亮的那天,当时间老人再次把大钟平均分开时,我会降临在灯火之城的金字塔前.带走那最珍贵的笑容."这是怪盗基德盗取巴黎卢浮宫的<蒙娜丽莎的微笑& ...
随机推荐
- loj#2305. 「NOI2017」游戏 2-sat
链接 https://loj.ac/problem/2305 https://www.luogu.org/problemnew/show/P3825 思路 3-sat神马的就不要想了,NP问题 除去x ...
- SpringBoot 使用Mybatis-Plus
简介 Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 特性 无侵入:Mybatis-Plus 在 My ...
- SpringBoot 利用过滤器Filter修改请求url地址
要求: 代码中配置的url路径为http://127.0.0.1/api/associates/queryAssociatesInfo 现在要求http://127.0.0.1/associates/ ...
- 题解—— 洛谷 p1269 信号放大器(贪心)
深刻的教训,不要写错读入 #include <cstdio> #include <algorithm> using namespace std; ; ; ,u[MAXM],v[ ...
- 17秋 SDN课程 第一次上机作业
第一题 拓扑: 测试连通性: 第二题 拓扑: 测试连通性: 第三题 拓扑: 测试连通性:
- Spring线程池
•依赖 spring-context-support •配置: <bean id ="taskExecutor" class ="org.springframewo ...
- 【Mysql】外键
MYSQL数据表建立外键 MySQL创建关联表可以理解为是两个表之间有个外键关系,但这两个表必须满足三个条件 1.两个表必须是InnoDB数据引擎 2.使用在外键关系的域必须为索引型(Index) 3 ...
- 性能测试 Performance Test Report
时间隔了很久,两年左右了吧,最近打开原来的测试报告,测试数据还是很漂亮的.TPS比我记忆中的要高很多. 数据中有些是定死了的(当时的要求),并发不是计算的,是用几个值跑起来试试看的.因为后期我们会用S ...
- Bootstrap 4正式发布还有意义吗?
历经三年开发,前端框架Bootstrap 4正式发布了.然而今天的Web世界已经和当初Mark Otto发布Bootstrap时的情况大为不同,一些开发者由此质疑它的更新是否还有意义. V4版本的主要 ...
- [JS]计算字符串中出现最多的字符和其出现次数
这是一道面试题 此处是利用Obj来解决的,当然不只此一种方法. //思路:遍历数组,拿到一个字符,并将之以 "字符":出现次数 的key:value形式存到对象中. //如果此字符 ...