题意:
给你两个字符串 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. qt 单元格加上边框_Excel如何自动添加边框?学会这个方法效率加倍!
  2. 加速点击控制应用中的边缘分析和机器学习部署 | 免费直播
  3. 为了远程控制:teamview检测为商用、ZeroTier + 自带远程桌面、Ping其他电脑ping不通
  4. java enum枚举的使用详情(实例与原理分析)
  5. 阅读器xodo_佐道Xodo——手机最佳pdf阅读器
  6. 为了上班摸鱼,我用Python开发“BOSS来了”!
  7. java jconsole_java中jconsole命令的学习
  8. C语言实现魔方阵代码及解析
  9. 层次分析法AHP - 代码注释多 - ( 数据建模 Python代码)
  10. hihocoder #1617 : 方格取数(dp)
  11. 在苹果Mac上找不到文件存储位置怎么办?
  12. Andriod中如何新建lunch项
  13. 什么是WAP?[wap全程认识]
  14. Codeforces Round #362 (Div. 2) D 树形dp
  15. ROSGazebo自定义多旋翼无人机仿真——环境搭建和模型建立
  16. 判断用户输入的8位信用卡号码是否合法
  17. 基于大数据的音乐数据中心平台(附:源码 课件 项目部署文档)
  18. 根据目标检测结果裁剪bbox保存到本地,python,opencv
  19. debian linux 修改时区
  20. 苹果交管局反馈信曝光 首次官方披露自动驾驶相关计划

热门文章

  1. 苹果、微软等巨头107道机器学习面试题/贪心学院
  2. Java 并发编程(一):摩拳擦掌
  3. 目标检测YOLO实战应用案例100讲-基于点云的三维多目标检测算法研究
  4. AC620FPGA学习笔记——PLL与NCO
  5. Windows10更新后,解决C盘空间占用过大的方法
  6. Cocos2dx中Scrollview控件滑动与其子控件监听冲突的解决方案(不改底层、最优)
  7. 分布式缓存:爱我你怕了吗?
  8. 计算机等级考试二级哪个科目比较容易过?看看学长怎么说
  9. 在抖音APP源码中如何实现播放器的音视频同步
  10. Android P90上ftell无法获取文件大小