Gym - 101864J Non Super Boring Substring(Manache + 树状数组)

B - Non Super Boring Substring(非超级无聊字串)

Gym - 101864J 

题面

You'll be given a string S and an integer K. You have to find the number of non super-boring substring inside S. A substring is called super-boring if it contains any palindromic substring of length ≥ K inside it.

For example, if S = “ababba” and K = 3, then the substring “abab” is a super-boring substring because it contains a palindrome “aba” whose length is ≥ 3. But the substring “bba” is a valid non super-boring substring.

Find the number of substrings which are not super-boring. Two substrings are considered different only if their starting or ending positions are different.

题目大意: 给你一个字符串 s 和一个整数 k, 找到尽可能多的子串使得它没有长度大于等于 k 的回文子串(子串的子串长度都小于k)

输入

第一行 t 代表测试数据的组数

每组数据第一行给出 k

第二行给出小写英文字符串 s

输出

每行输出字符串的个数

数据范围

1 \le t \le 100

k \le \lvert s \rvert \le 10^5

所有测试样例的字符串长度加起来总共不超过10^6

测试样例

输入1

 3
 3
 bababe
 4
 abcbbcbc
 3
 aabacecc

输出1

 12
 27
 20

思路

只要求得在i处以i为右端点的字串中含有长度大于等于k的子串的个数f(i)。

则答案 为 ans = \sum_{i = 1}^n (i-f(i)) 。

下面用Manache 和 树状数组求这样一个f(i) 数组:

先用Manache求以 i 为中心的极大回文子串长度 d(i)。

用 i 遍历每个字串中心,考虑一个以 i 为中心的长度大于 k 的极大回文子串对 f(i) 的贡献:

  • 设子串的左边界 l 和右边界 r , 则该子串首先对右端点为 r 的字串产生贡献,所有 (l_0, r), 0 \le l_0 \le l 的字串都将含有k长回文字串, 即: f(r) = l + 1 (下标起点为0的情况下)。

  • 当多个回文字串有同一个右端点时,应该取最右边的l,即 f(r)取最大值: f(r) = max(l + 1), f(i) > k 。

  • 其次子串还会对右端点大于 r 的子串产生贡献,在没有新的子串做更大的贡献的情况下,会继承该字串的f(i) ;如果有更大的就取更大的,于是 f(i) 呈现出非降序的特点。

想到了什么? 树状数组。

树状数组存前缀区间上的最大值,求和的时候查询即可。

代码

 #include <iostream>
 #include <algorithm>
 #include <cstring>
 #include <vector>
 ​
 using namespace std;
 ​
 typedef long long ll;
 ​
 const int MAXL = 1e5 + 10;
 char s[MAXL << 1];
 int d[MAXL << 1], t, k;
 int tree[MAXL];
 ​
 void read() {
     memset(s, 0, sizeof(s));
     memset(tree, 0, sizeof(tree));
     cin >> k;
     char tmp[MAXL]; cin >> tmp;
     int n = strlen(tmp);
     for (int i = 0; i < n; ++i) {
         s[2 * i] = '#';
         s[2 * i + 1] = tmp[i];
     }
     s[2 * n] = '#';
 }
 ​
 void Manache() {
     int n = strlen(s);
     fill(d, d + n - 1, 1);
     for (int i = 0, l = 0, r = -1; i < n; ++i) {
         int k = (i > r)? 1 : min(d[l + r - i], r - i + 1);
         while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) ++k;
         d[i] = k--;//这里的自减代表不同的意义,前置自减得到的d不包括i本身,后置的包括i本身
         if (i + k > r) {
             l = i - k;
             r = i + k;
         }
     }
 }
 ​
 int lowbit(int x) {
     return x & -x;
 }
 ​
 void add(int x,int d,int n){//单点修改,维护最大值
     for(int i = x; i <= n; i += lowbit(i))
         tree[i] = max(tree[i], d);
 }
 ​
 int query(int x){//查询前缀最大值
     int ans = 0;
     for(int i = x; i > 0; i -= lowbit(i))
         ans = max(tree[i], ans);
     return ans;
 }
 ​
 int main() {
     ios::sync_with_stdio(false);
     cin.tie(0); cout.tie(0);
     cin >> t;
     while(t--) {
         read();
         Manache();
         //以i点作为右端点的串有多少个包含k长回文串
         int len = strlen(s);
         for (int i = 1; i < len - 1; ++i) {//遍历以i为中心的回文串
             if (d[i] - 1 < k) continue;//长度小于k的会问字串不用考虑
             int l, r, minl = k;//取出回文串的起点终点, 最小回文串长度
             if ((minl & 1) != ((d[i] - 1)& 1)) ++minl;//以i为中心长度大于k的最小回文串
             if (i & 1) {//i为奇数,则d[i]为原串i / 2处长度为奇数极大回文串长度
                 r = i / 2 + minl / 2;//该回文串的右端
                 l = i / 2 - minl / 2;//该回文串的左端
             }
             else {//偶数
                 r = i / 2 + minl / 2 - 1;
                 l = i / 2 - minl / 2;
             }
             //之前错误的原因:树状数组是严格从1开始的
             add(r + 1, l + 1, len / 2);//以i为右端点的子串含有大于k长回文串的个数
         }
         ll ans = 0;
         for (int i = 1; i <= len / 2; ++i) 
             ans += (ll)(i - query(i));
         cout << ans << endl;
     }
     return 0;
 }

注:由于树状数组的下标从1开始,字符串也应该下标从1开始,我这里从0开始纯粹时因为一开始写错了懒得改。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值