Three Palindromes

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1948    Accepted Submission(s): 687

Problem Description
Can we divided a given string S into three nonempty palindromes?
 
Input
First line contains a single integer T≤20 which denotes the number of test cases.

For each test case , there is an single line contains a string S which only consist of lowercase English letters.1≤|s|≤20000

 
Output
For each case, output the "Yes" or "No" in a single line.
 
 
Sample Input
2
abc
abaadada
 
 
Sample Output
Yes
No

题意:给出一个字符串,问能否将字符串分为三段,并且每一段的是回文串。

思路:这题要用到manacher算法,不知道的可以看一下这篇博客:https://blog.csdn.net/dyx404514/article/details/42061017。

首先用manacher求出以每个点为中点的回文串的半径,然后,我们可以知道,要将字符串分为三段,那第一段一定包含第一个字符,第三段一定包含最后一个字符。然后判断第一个回文串和第三个之间剩下的字符是否构成回文串就行了。

所以,我们找到用manacher算法求出的所有回文串中,包含了第一个字符和和最后一个字符的有那些,然后两两配对,看看是否存在某一对,他们不重合,且之间剩下的字符也是回文串。

具体操作看代码:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<string>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<stack>
  8. #include<queue>
  9. #define eps 1e-7
  10. #define ll long long
  11. #define inf 0x3f3f3f3f
  12. #define pi 3.141592653589793238462643383279
  13. using namespace std;
  14. const int maxn = ;
  15. char a[maxn],s_new[maxn<<];
  16. int s_len[maxn<<];
  17.  
  18. int Init() //manacher算法初始化(模板)
  19. {
  20. int len = strlen(a);
  21. s_new[] = '@';
  22. int j = ;
  23. for(int i=; i<len; ++i) //在每个字符左右插入'#'
  24. {
  25. s_new[j++] = '#';
  26. s_new[j++] = a[i];
  27. }
  28. s_new[j++] = '#';
  29. s_new[j] = '\0';
  30. return j;
  31. }
  32.  
  33. void manacher(int len) //计算以每个点为中心的回文串的半径(模板)
  34. {
  35. int high = ,flag = ;
  36. for(int i=; i<len; ++i)
  37. {
  38. if(i < high)
  39. s_len[i] = min(high-i+ , s_len[*flag-i]);
  40. else
  41. s_len[i] = ;
  42.  
  43. while(s_new[i + s_len[i]] == s_new[i - s_len[i]])
  44. s_len[i]++;
  45.  
  46. if(i + s_len[i] - > high)
  47. {
  48. high = i+ s_len[i] -;
  49. flag = i;
  50. }
  51. }
  52. return;
  53. }
  54.  
  55. bool judge(int len) //判断是否可以分为3个回文串
  56. {
  57. int visit1[maxn<<], visit2[maxn<<], cnt1 = , cnt2 = ;
         //visit1用来存储所有包含第一个字符的字符串的中点下标
  58. //visit2用来存储包含最后一个字符串的中点下标
  59.  
  60. for(int i=; i<len; ++i) //遍历一遍,找到存入visit数组中
  61. {//因为以 i 为中点的回文串的长度是s_len[i]-1,所以还要判断一下这个回文串长度是否为 0;
  62. if(i - s_len[i] + == && s_len[i] - > )
  63. visit1[cnt1++] = i;
  64. if(i + s_len[i] - == len- && s_len[i] - > )
  65. visit2[cnt2++] = i;
  66. }
  67. bool flag = false;
  68. int mid;
  69. for(int i=; i<cnt1; ++i) //for循环的嵌套用来让三段中,第一段和第三段两两配对
  70. {
  71. for(int j=cnt2-; j>=; --j)
  72. {
  73. if(visit1[i] + s_len[visit1[i]] - < visit2[j] - s_len[visit2[j]] + )
              //选出的两段不能有重合,有重合表示中间没字符了
  74. {
  75. mid = ( (visit1[i] + s_len[visit1[i]] - ) + (visit2[j] - s_len[visit2[j]] + ) ) / ;
                //然后取两段中间剩余字符的中点
  76. if(mid - s_len[mid] + <= visit1[i] + s_len[visit1[i]] && s_len[mid]- > )
                //判断以中点为中心的回文串是否完全覆盖了中间所有的字符
  77. {
  78. flag = true; //若覆盖了,则表示可以分成三段回文串
  79. break; //就不需要再继续查找了
  80. }
  81. }
  82. }
  83. if(flag) break;
  84. }
  85. return flag;
  86. }
  87.  
  88. int main()
  89. {
  90. int t;
  91. cin>>t;
  92. while(t--)
  93. {
  94. scanf("%s",a);
  95. int len = Init();
  96. manacher(len);
  97.  
  98. bool T_or_F = judge(len);
  99. if(T_or_F)
  100. cout<<"Yes\n";
  101. else
  102. cout<<"No\n";
  103. }
  104. return ;
  105. }

hdu5340—Three Palindromes—(Manacher算法)——回文子串的更多相关文章

  1. zoj 2744 Palindromes(计算回文子串个数的优化策略)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2744 题目描述: A regular palindrome i ...

  2. cf#516C. Oh Those Palindromes(最多回文子串的字符串排列方式,字典序)

    http://codeforces.com/contest/1064/problem/C 题意:给出一个字符串,要求重新排列这个字符串,是他的回文子串数量最多并输出这个字符串. 题解:字典序排列的字符 ...

  3. manacher算法——回文串计算的高效算法

    manacher算法的由来不再赘述,自行百度QWQ... 进入正题,manacher算法是一个高效的计算回文串的算法,回文串如果不知道可以给出一个例子:" noon ",这样应该就 ...

  4. 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  5. lintcode最长回文子串(Manacher算法)

    题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...

  6. 1089 最长回文子串 V2(Manacher算法)

    1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 回文串是指aba.abba.cccbccc.aaaa ...

  7. 51nod1089(最长回文子串之manacher算法)

    题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 题意:中文题诶~ 思路: 我前面做的那道回文子串的题 ...

  8. 求最长回文子串:Manacher算法

    主要学习自:http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html 问题描述:回文字符串就是左右 ...

  9. Manacher's algorithm: 最长回文子串算法

    Manacher 算法是时间.空间复杂度都为 O(n) 的解决 Longest palindromic substring(最长回文子串)的算法.回文串是中心对称的串,比如 'abcba'.'abcc ...

  10. 【转】最长回文子串的O(n)的Manacher算法

    Manacher算法 首先:大家都知道什么叫回文串吧,这个算法要解决的就是一个字符串中最长的回文子串有多长.这个算法可以在O(n)的时间复杂度内既线性时间复杂度的情况下,求出以每个字符为中心的最长回文 ...

随机推荐

  1. 一个单元测试 学习 aysnc await

    using System; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; name ...

  2. oracle 跨库访问

    创建DBLINK的方法: 1. create public database link dblink connect to totalplant identified by totalplant us ...

  3. Centos编译Redis4.0.9源码过程记录

    mkdir /home/redis cd /home/redis 下载源码 wget https://codeload.github.com/antirez/redis/tar/4.0.9 解压源码包 ...

  4. PyDev for eclipse 插件下载地址

    PyDev for eclipse 插件下载地址http://sourceforge.net/projects/pydev/files/pydev/python解释器以及python类库下载地址htt ...

  5. KVC & KVO 入门

    KVC: 简介: 全称 Key-Value Coding .KVC是一种间接访问对象属性(用字符串表征)的机制,而不是直接调用对象的accessor(setter/getter)方法或是直接访问成员对 ...

  6. 基于sersync海量文件实时同步

    项目需求:最近涉及到数百万张图片从本地存储迁移到云存储,为了使完成图片迁移,并保证图片无缺失,业务不中断,决定采用实时同步,同步完后再做流量切换.在实时同步方案中进行了几种尝试. 方案1:rsync+ ...

  7. javascript 特定字符分隔字符串函数

    function fn(num,div,token){//num需要分割的数字,div多少位分割 token分割字符 num=num+'',div=div||3,token=token||',' re ...

  8. Apache Shiro学习-2-Apache Shiro Web Support

     Apache Shiro Web Support  1. 配置 将 Shiro 整合到 Web 应用中的最简单方式是在 web.xml 的 Servlet ContextListener 和 Fil ...

  9. list.contains

    list.contains(o),系统会对list中的每个元素e调用o.equals(e),方法,加入list中有n个元素,那么会调用n次o.equals(e),只要有一次o.equals(e)返回了 ...

  10. Cocoa Touch(四): 多线程GCD, NSObject, NSThread, NSOperationQueue

    多线程的重要性不必多言,现代操作系统不可能离开进程线程的抽象.具体到ios应用,我们只能在一个进程中管理线程,主线程不应该去执行非常耗时间的后台操作导致出现卡机现象,后台的事情交给后台线程来完成. G ...