题目链接

题目大意是问在$S$串中找区间$[i,j]$,在$T$串中找位置$k$,使得$S[i,j]$和$T[1,k]$可以组成回文串,并且$j-i+1>k$,求这样的三元组$(i,j,k)$的个数。

一开始有点懵,但是仔细一想,因为$j-i+1>k$,所以$S[i,j]$中一定包含了回文串后半段的一部分,即$S[i,j]$中一定有后缀是回文串。

如果回文串是$S[x,j]$,则剩余的$S[i,x-1]$与$T[1,k]$应该也能组成回文串。如果将串$S$倒置,则串$S^{'}$上的原$S[i,x-1]$位置与$T[1,k]$应该相同。

所以解题方式应该比较明了,将串$S$倒置,然后求扩展$kmp$,得到串$S^{'}$每个后缀与串$T$的最长公共前缀。然后对串$S^{'}$构建回文自动机。

可以得到串$S^{'}$每个位置作为回文子串的结尾时的回文串个数。然后枚举串$S^{'}$每个位置$i$,以当前位置作为上文中的$x$,然后计算当前位置对答案的贡献。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int maxn = 1e6 + ;
  5. int Next[maxn];
  6. int Ex[maxn];
  7. void getN(char* s1) {//求子串与自身匹配
  8. int i = , j, p, len = strlen(s1);
  9. Next[] = len;
  10. while (i + < len && s1[i] == s1[i + ])
  11. i++;
  12. Next[] = i;
  13. p = ;
  14. for (i = ; i < len; i++) {
  15. if (Next[i - p] + i < Next[p] + p)
  16. Next[i] = Next[i - p];
  17. else {
  18. j = Next[p] + p - i;
  19. if (j < )
  20. j = ;
  21. while (i + j < len && s1[j] == s1[i + j])
  22. j++;
  23. Next[i] = j;
  24. p = i;
  25. }
  26. }
  27. }
  28. void getE(char* s1, char* s2) {//求子串与主串匹配
  29. int i = , j, p, len1 = strlen(s1), len2 = strlen(s2);
  30. while (i < len1 && i < len2 && s1[i] == s2[i])
  31. i++;
  32. Ex[] = i;
  33. p = ;
  34. for (i = ; i < len1; i++) {
  35. if (Next[i - p] + i < Ex[p] + p)
  36. Ex[i] = Next[i - p];
  37. else {
  38. j = Ex[p] + p - i;
  39. if (j < )
  40. j = ;
  41. while (i + j < len1 && j < len2 && s1[i + j] == s2[j])
  42. j++;
  43. Ex[i] = j;
  44. p = i;
  45. }
  46. }
  47. }
  48. struct Palindromic_Tree {
  49. int next[maxn][];//指向的串为当前串两端加上同一个字符构成
  50. int fail[maxn];//fail指针,失配后跳转到fail指针指向的节点
  51. int cnt[maxn]; //表示节点i表示的本质不同的串的个数,最后用count统计
  52. int num[maxn]; //表示节点i表示的最长回文串的最右端点为回文串结尾的回文串个数
  53. int len[maxn];//len[i]表示节点i表示的回文串的长度
  54. int id[maxn];//表示数组下标i在自动机的哪个位置
  55. int S[maxn];
  56. int last;//指向上一个字符所在的节点,方便下一次add
  57. int n; int p;
  58. int newnode(int x) {
  59. for (int i = ; i < ; ++i) next[p][i] = ;
  60. cnt[p] = ; num[p] = ; len[p] = x;
  61. return p++;
  62. }
  63. void init() {//初始化
  64. p = ;
  65. newnode(); newnode(-);
  66. last = ; n = ;
  67. S[n] = -;
  68. fail[] = ;
  69. }
  70. int get_fail(int x) {//失配后找一个最长的
  71. while (S[n - len[x] - ] != S[n]) x = fail[x];
  72. return x;
  73. }
  74. void add(int x) {
  75. S[++n] = x;
  76. int cur = get_fail(last);//通过上一个回文串找这个回文串的匹配位置
  77. if (!next[cur][x]) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
  78. int now = newnode(len[cur] + );//新建节点
  79. id[n - ] = now;
  80. fail[now] = next[get_fail(fail[cur])][x];//建立fail指针,以便失配后跳转
  81. next[cur][x] = now;
  82. num[now] = num[fail[now]] + ;
  83. }
  84. else
  85. id[n - ] = next[cur][x];
  86. last = next[cur][x];
  87. cnt[last]++;
  88. }
  89. void count() {
  90. for (int i = p - ; i >= ; --i) cnt[fail[i]] += cnt[i];
  91. }
  92.  
  93. }a;
  94. char s[maxn], s1[maxn], t[maxn];
  95. int main() {
  96. scanf("%s%s", s, t);
  97. int n = strlen(s), m = strlen(t);
  98. for (int i = ; i < n; i++)
  99. s1[i] = s[n - i - ];
  100. getN(t);
  101. getE(s1, t);
  102. a.init();
  103. for (int i = ; i < n; i++)
  104. a.add(s1[i] - 'a');
  105. a.count();
  106. ll ans = ;
  107. for (int i = n - ; i >= ; i--) {
  108. int w = Ex[i];
  109. ans += 1LL * w * a.num[a.id[i - ]];
  110. }
  111. printf("%lld\n", ans);
  112. }

[gym101981M][2018ICPC南京M题]Mediocre String Problem的更多相关文章

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

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

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

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

  3. Gym - 101981M:(南京) Mediocre String Problem(回文树+exkmp)

    #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) using ...

  4. 2018ACM-ICPC南京区域赛M---Mediocre String Problem【exKMP】【Manacher】

    这题就单独写个题解吧.想了两天了,刚刚问了一个大佬思路基本上有了. 题意: 一个串$S$,一个串$T$,在$S$中选一段子串$S[i,j]$,在$T$中选一段前缀$T[1,k]$使得$S[i,j]T[ ...

  5. Gym - 101981M The 2018 ICPC Asia Nanjing Regional Contest M.Mediocre String Problem Manacher+扩增KMP

    题面 题意:给你2个串(长度1e6),在第一个串里找“s1s2s3”,第二个串里找“s4”,拼接后,是一个回文串,求方案数 题解:知道s1和s4回文,s2和s3回文,所以我们枚举s1的右端点,s1的长 ...

  6. [gym101981D][2018ICPC南京D题]Country Meow

    题目链接 题目大意是求三维空间可以包含$n$个点的最小圆半径. 如果有做过洛谷P1337就会发现这到题很模拟退火,所以就瞎搞一发. $PS:$注意本题时限$3$秒. #include<bits/ ...

  7. hdu String Problem(最小表示法入门题)

    hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...

  8. bestcoder 48# wyh2000 and a string problem (水题)

    wyh2000 and a string problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K ...

  9. 2018ICPC南京网络赛

    2018ICPC南京网络赛 A. An Olympian Math Problem 题目描述:求\(\sum_{i=1}^{n} i\times i! \%n\) solution \[(n-1) \ ...

随机推荐

  1. 通用DES加密解密方法

    /// <summary> /// DES加密方法 /// </summary> /// <param name="strPlain">明文&l ...

  2. POJ 3068 运送危险化学品 最小费用流 模板题

    "Shortest" pair of paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1215 ...

  3. Python3学习笔记(十三):装饰器

    装饰器就是一个闭包,它的主要作用是在不改变原函数的基础上对原函数功能进行扩展. 我们先来写一个简单的函数: from time import sleep def foo(): print(" ...

  4. POJ 6621: K-th Closest Distance(主席树 + 二分)

    K-th Closest Distance Time Limit: 20000/15000 MS (Java/Others)    Memory Limit: 524288/524288 K (Jav ...

  5. VS2015 ASP.NET MVC5 EntityFramework6 Oracle 环境篇

    //来源:https://www.cnblogs.com/lauer0246/articles/9576940.html Asp.Net MVC EF各版本区别 2009年發行ASP.NET MVC ...

  6. Java权限管理

    基于角色的权限管理

  7. Shell执行脚本

    Shell作用是解释执行用户的命令,用户输入一条命令,Shell就解释执行这一条,这种方式称为交互式,但还有另一种执行命令的方式称为批处理方式,用户事先写一个Shell脚本,Shell可以一次把这些命 ...

  8. LeetCode 92. 反转链表 II(Reverse Linked List II)

    题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL, ...

  9. JSP——JSTL定制标签 - 递归标签显示属性结构

    编写定制标签分为三个步骤:编写标签处理器.配置标签.使用标签. 1.标签处理器 标签处理器和标签是一一对应的关系 package com.oolong.utils.customtags;   impo ...

  10. 为解决Thymeleaf数字格式化问题而想到的几种方案

    背景: spring后端输出double类型数据,前端使用thymeleaf框架,格式化double数据类型,由于需要显示原值(比如原来录入5,而不能显示5.00),因此需要存储数值(存储值为deci ...