题意:
给你两个字符串 s , t s,t s,t,要求从 s s s中找到一个子串和 t t t的一个前缀拼起来,结果要是回文串。求多少种拼法。

思路:
借此题复习了一下字符串算法。
首先 s s s中找到的子串可以分为两部分: s 1 + s 2 s1+s2 s1+s2, s 1 s1 s1一定是 t t t一个前缀的逆反串, s 2 s2 s2一定是个回文串。

所以可以将一开始所给的 s s s串反一下,然后算出每个右端点 i i i对应回文串的个数,再找到 [ i + 1 , l e n ] [i+1,len] [i+1,len]部分与 t t t串的最长公共前缀,二者相乘就是答案。

算回文串的过程可以用马拉车,算LCP的过程可以用exkmp。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
#include <unordered_map>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <queue>
using namespace std;typedef long long ll;
typedef unsigned long long ull;
const int maxn = 3e6 + 7;
int n, m, z[maxn], p[maxn];
char a[maxn], aa[maxn], b[maxn], c[maxn];
int f[maxn],sum[maxn];
inline void Z(char *s, int n) {for (int i = 1; i <= n; i++) z[i] = 0;z[1] = n;for (int i = 2, l = 0, r = 0; i <= n; i++) {if (i <= r) z[i] = min(z[i-l+1], r - i + 1);while (i + z[i] <= n && s[i+z[i]] == s[z[i]+1]) ++z[i];if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;}
}inline void exkmp(char *s, int n, char *t, int m) {Z(t, m);for (int i = 1; i <= n; i++) p[i] = 0;for (int i = 1, l = 0, r = 0; i <= n; i++) {if (i <= r) p[i] = min(z[i-l+1], r - i + 1);while (i + p[i] <= n && s[i+p[i]] == t[p[i]+1]) ++p[i];if (i + p[i] - 1 > r) l = i, r = i + p[i] - 1;}
}void manacher() {int id = 0,mx = -1;int l = 0;for(int i = 1;i <= n;i++) {c[++l] = '#';c[++l] = a[i];}c[++l] = '#';for(int i = 1;i <= l;i++) {if(id + mx > i)f[i] = min(f[2 * id - i],id + mx - i);while(i - f[i] >= 1 && i + p[i] <= l && c[i - f[i]] == c[i + f[i]])f[i]++;if(id + mx < i + f[i]) {id = i;mx = f[i];}}for(int i = 1;i <= l;i++) {sum[i]++;sum[i + f[i]]--;}int now = 0;ll ans = 0;for(int i = 1;i <= l;i++) {now += sum[i];if(c[i] == '#') continue;if(now > 0) {ans += 1ll * now * p[i / 2 + 1];}}printf("%lld\n",ans);
}int main() {scanf("%s%s",aa + 1,b + 1);n = strlen(aa + 1);m = strlen(b + 1);for(int i = 1;i <= n;i++) {a[i] = aa[n - i + 1];}exkmp(a, n, b, m);manacher();return 0;
}

2018ICPC南京 Problem M. Mediocre String Problem(回文串,马拉车,扩展KMP)相关推荐

  1. 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 ...

  2. Problem M. Mediocre String Problem

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

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

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

  4. 最长回文串 马拉车算法 C++

    最长回文串 LeetCode 5.最长回文串 给你一个字符串 s,找到 s 中最长的回文子串. 示例 1: 输入:s = "babad" 输出:"bab" 解释 ...

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

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

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

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

  7. 2018ICPC 南京 Mediocre String Problem 扩展KMP + Manacher

    题目大意为计算 S S S 串的子串 s s s 串和 T T T 中的前缀 t t t 串拼起来是回文串的种数的总贡献,要求 s s s 串长度大于 t t t, s + t s+t s+t 是回文 ...

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

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

  9. ICPC 2018 南京 Mediocre String Problem

    题解: 题目的意思就是在第一个串里找"s1s2s3",第二个串里找"s4",如上拼接后,是一个回文串,求方案数 可以发现,s1与s4是回文的,s2和s3是回文的 ...

最新文章

  1. 尺度不变特征变换匹配算法详解
  2. C#学习视频分享与开发技术QQ交流群
  3. svn 设置post-commit后 报错svn: Can't convert string from 'UTF-8' to native encoding
  4. python整形浮点型运算规则
  5. 20201202 《计算感知》武老师 第2节课 笔记
  6. calendar类_带有时区的字符怎样转换为时间及Java 8中日期 与 Calendar 转换
  7. jQuery自定义选择器
  8. Go语言学习Day02
  9. 软件人员kpi制定模板_软件科技公司绩效考核办法模板
  10. JS 仿淘宝幻灯片 非完整版 小案例
  11. 关于Linux系统下若干易混淆目录的分析
  12. 计算机共享打印机后重启才能够打印的解决方法
  13. 通过Modbus转EtherNetIP网关连接AB PLC的配置案例
  14. CDH集群更换IP处理方法
  15. 发那科机器人回原位置先上升_发那科机器人offset condition 指令什么意思
  16. 谢烟客---------Linux之深入理解anaconda使用
  17. python机器学习实现oneR算法 以鸢尾data为例
  18. 恋人日记服务器维护中,知乐日记:恋人不会在最终相遇 因为他们本来就一直在一起...
  19. css3的clip-path方法裁切图片(三角形,多边形,圆,椭圆)
  20. Virtual box安装回退的一系列可能的原因及解决办法

热门文章

  1. Docker 删除容器镜像 /加载镜像
  2. 纯真的年代 暖暖的亲情——我眼中的彼得(电影《纳尼亚传奇》观后感)
  3. Related work怎么写
  4. 【xlwings api语言参考】Range.ShrinkToFit 属性
  5. 一个程序员创业一年的感悟
  6. python argparse模块详解_python自学argparse模块
  7. Snipaste吸取色值
  8. 《计算机视觉中的多视图几何》学习笔记
  9. 房产经纪人证报考资格是什么?需要哪些条件
  10. PPT打印技巧——你还在一页打六张吗?