Mediocre String Problem

time limit per test:1 seconds memory limit per test:256 megabytes

Problem Description

Given two strings s and t, count the number of tuples ( i , j , k ) (i, j, k) (i,j,k) such that

  1. 1 ≤ i ≤ j ≤ ∣ s ∣ 1 ≤ i ≤ j ≤ |s| 1≤i≤j≤∣s∣
  2. 1 ≤ k ≤ ∣ t ∣ . 1 ≤ k ≤ |t|. 1≤k≤∣t∣.
  3. j − i + 1 > k j − i + 1 > k j−i+1>k.
  4. The i i i-th character of s to the j j j-th character of s s s, concatenated with the first character of t t t to the k k k-th character of t t t, is a palindrome.

A palindrome is a string which reads the same backward as forward, such as “abcba” or “xyzzyx”.

Input

The first line is the string s s s ( 2 ≤ ∣ s ∣ ≤ 1 0 6 2 ≤ |s| ≤ 10^6 2≤∣s∣≤106). The second line is the string t t t ( 1 ≤ ∣ t ∣ < ∣ s ∣ 1 ≤ |t| < |s| 1≤∣t∣<∣s∣). Both s and t t t contain only lower case Latin letters.

Output

The number of such tuples.

Sample Input

aabbaa
aabb

Sample Output

7

题意

有两个字符串s和t,计算满足条件的三元组 ( i , j , k ) (i,j,k) (i,j,k)的数量,三元组需满足:

  1. 1 ≤ i ≤ j ≤ ∣ s ∣ 1 ≤ i ≤ j ≤ |s| 1≤i≤j≤∣s∣
  2. 1 ≤ k ≤ ∣ t ∣ . 1 ≤ k ≤ |t|. 1≤k≤∣t∣.
  3. j − i + 1 > k j − i + 1 > k j−i+1>k.
  4. 将串 s s s的第 i i i到 j j j个字符取出,将 t t t的第 1 1 1到 k k k个字符取出,两者拼接在一起,新的串是回文串。
思路

从 t t t中截取的 t [ 1.. k ] t[1..k] t[1..k],需要与s串的 s [ i . . . j ] s[i...j] s[i...j]段组成回文串,则需要 s [ i ] = t [ k ] , s [ i + 1 ] = t [ k − 1 ] . . . . . s [ i + k − 1 ] = t [ 1 ] s[i]=t[k],s[i+1]=t[k-1].....s[i+k-1] = t[1] s[i]=t[k],s[i+1]=t[k−1].....s[i+k−1]=t[1].当确定 i i i和 k k k后,此时还需要 s [ i + k . . . . j ] s[i+k....j] s[i+k....j]段为回文串,统计 s s s的所有回文子串中,以 i + k i+k i+k为左端点的回文子串的数量a即可。
a数组,可以利用manachar算法O(n)计算:
求出以每个位置为中心的最大回文半径,计算出其最靠左的位置 l l l,( l l l为原串中的下标,下同),和中心的下标 m i d mid mid(若回文串长度为偶数,则 m i d mid mid为对称轴左边的第一个字符的下标)。利用差分 a [ l ] + + , a [ m i d + 1 ] − − a[l]++,a[mid+1]-- a[l]++,a[mid+1]−−。最后遍历一下,即可求出a数组。

现在只需要确定 i + k − 1 i+k-1 i+k−1即可,枚举 i + k − 1 i+k-1 i+k−1的位置,求出有多少个 k k k满足上述条件。将串s翻转,求出串 s ′ s' s′的每个后缀与串 t t t的最长公共前缀b,b即所求。可以利用exkmp求出b数组。

a n s = ∑ i = 1 ∣ s ∣ − 1 a [ i − 1 ] ∗ b [ i ] ans = \sum_{i=1}^{|s|-1}a[i-1]*b[i] ans=∑i=1∣s∣−1​a[i−1]∗b[i]。

PS:因为s串翻转了,所以a也需要翻转一下。或者可以先翻转串s,再求a数组,不过这里变成了求s’的所有回文子串中,以 i i i为右端点的回文子串的数量。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<iterator>
#include<bitset>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define eps 1e-6using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1002000;
const int mod = 1000000007;
char s[maxn], t[maxn], str[maxn*2];
int slen, tlen, nex[maxn], exnex[maxn], p[maxn*2], a[maxn];
int Insert();
void getnex();
void manachar();
void getextend();int main()
{int i, j, k;LL ans = 0;scanf("%s %s", s, t);slen = strlen(s), tlen = strlen(t);manachar();getnex();getextend();for(i=1;i<slen;i++)ans += 1LL*a[i-1]*exnex[i];printf("%lld\n", ans);return 0;
}void manachar()
{int len = Insert(), i, j, id, mx;mx = 0;for(i=1;i<=len;i++){p[i] = mx>i?min(mx-i, p[2*id-i]):1;while(str[i+p[i]] == str[i-p[i]])p[i]++;int l = (i-p[i]+1)/2, r = i/2-1;if(p[i] >= 2)a[l]++, a[r+1]--;if(i+p[i]>mx){id = i;mx = i+p[i];}}for(i=1;i<=slen;i++)a[i] += a[i-1];for(i=0;i<slen-1-i;i++){swap(s[i], s[slen-1-i]);swap(a[i], a[slen-1-i]);}
}int Insert()
{int i, j = 2;str[0] = '*', str[1] = '#';for(i=0;i<slen;i++){str[j] = s[i];str[j+1] = '#';j+=2;}str[j] = 0;return j;
}void getnex()
{int i, j, po;nex[0] = tlen;i=0;while(i+1<tlen && t[i] == t[i+1])i++;nex[1] = i;po = 1;for(i=2;i<tlen;i++){if(nex[i-po]+i < nex[po] + po)nex[i] = nex[i-po];else{j = nex[po]+po-i;if(j<0)j = 0;while(i+j<tlen && t[i+j] == t[j])j++;nex[i] = j;po = i; }}
}//计算exnext数组
void getextend()
{int i=0, j, po;slen = strlen(s);while(s[i] == t[i] && i<slen && i<tlen)i++;exnex[0] = i;po = 0;for(i=1;i<slen;i++){//exnex[po]+po代表匹配到过的最远位置,//nex[i-po]代表在t中匹配到的位置, if(nex[i-po]+i < exnex[po]+po)exnex[i] = nex[i-po];else{j = exnex[po]+po-i;if(j<0)j=0;while(i+j<slen &&j<tlen && s[i+j] == t[j])j++;exnex[i] = j;po = i; }}
}

Gym_101981M Mediocre String Problem(exkmp+manachar)相关推荐

  1. 2018 ICPC 南京 M. Mediocre String Problem(ExKMP + Manacher / ExKMP+回文树)

    2018 ICPC 南京 全文见:https://blog.csdn.net/qq_43461168/article/details/112796538 M. Mediocre String Prob ...

  2. Problem M. Mediocre String Problem(Z 函数 + PAM)

    Problem M. Mediocre String Problem 给定两个串s,ts, ts,t,要求有多少不同的三元组(i,j,k)(i, j, k)(i,j,k),满足: 1≤i≤j≤∣s∣1 ...

  3. M - Mediocre String Problem( 扩展KMP + Manacher + 差分 )

    M - Mediocre String Problem( 扩展KMP + Manacher + 差分 ) 题意:给出一个串S,和一个串T. 要求 从S串中取一个子串,后面接上T串的一个前缀 组成一个结 ...

  4. Mediocre String Problem(马拉车+扩展KMP)

    文章目录 [Mediocre String Problem](https://codeforces.com/gym/101981) 题目大意 解题思路 代码 Mediocre String Probl ...

  5. ACM-ICPC2018南京赛区 Mediocre String Problem

    Mediocre String Problem 题解: 很容易想到将第一个串反过来,然后对于s串的每个位置可以求出t的前缀和它匹配了多少个(EXKMP 或者 二分+hash). 然后剩下的就是要处理以 ...

  6. Gym - 101981 Problem M. Mediocre String Problem (扩展KMP + Manacher)

    Problem M. Mediocre String Problem 题目链接:https://vjudge.net/problem/Gym-101981M 题目大意:给出两个串S,T,从S中选择 i ...

  7. Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢Grunt大佬的细心讲解)...

    layout: post title: Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细 ...

  8. Problem M. Mediocre String Problem

    Problem M. Mediocre String Problem(马拉车+拓展KMP) 题意:给一个S串一个T串, 问有多少个F(i, j, k),F(i, j, k) 的定义是S串选个下标i~j ...

  9. Mediocre String Problem Gym - 101981M

    https://cn.vjudge.net/problem/Gym-101981M 很容易tle #include <iostream> #include <cstring> ...

最新文章

  1. 微服务架构_企业中的微服务:敌是友?
  2. linux下的动态链接库和静态链接库到底是个什么鬼?(一)静态链接库的编译与使用...
  3. STP文件服务器,综合监控stp服务器
  4. 关于MySQL出现锁等待lock wait timeout exceeded; try restarting transaction 的解决方案
  5. LInux查看文件内容
  6. 信号处理深度学习机器学习_机器学习与信号处理
  7. jzoj3382-七夕祭【贪心,中位数】
  8. Java多线程系列(五):线程池的实现原理、优点与风险、以及四种线程池实现
  9. 【记录】【解决方案】java发邮件错误:Couldn‘t connect to host, port: localhost, 25; timeout -1;易邮SMTP服务器无法启动;
  10. 论文写作-如何设置页眉奇偶页显示不同
  11. 怎么把flac转换为mp3格式
  12. 秀米数字编号实用知识点
  13. 项目日志20190704
  14. wifi营销小程序源码+搭建教程
  15. 读书札记-曾国藩陈景润
  16. Matlab的一些术语
  17. 喜马拉雅如何正序播放
  18. Elasticsearch 论坛实战-基于tie_breaker参数优化dis_max搜索效果
  19. js中关于时间格式转化,时间大小比较的方法
  20. Python 求交错序列前N项和

热门文章

  1. GitHub的Windows客户端的使用教程
  2. 【Python计量】异方差性的处理
  3. 如何用LLMs来赚钱?基于ChatGPT的商业模式指南
  4. VMware虚拟网络网络模式
  5. css 前端 吸取颜色 附件截图工具 snipaste
  6. python requests post 二进制流_Python requests 模块
  7. 货运物流app开发 为货主和物流公司提供一个方便的平台
  8. linux 迅雷 命令行,Linux系统下使用wine运行迅雷5的方法
  9. 如何将chatGpt接入企业微信
  10. Python函数式编程进阶