Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.

For example:

Given "aacecaaa", return "aaacecaaa".

Given "abcd", return "dcbabcd".

思路:

写了三份代码,各种超时。知道肯定要用之前学了几遍也记不住的方法了--判断最长回文的O(N)算法Manacher算法。

特别注意:

s[i]这样的写法非常耗时!

  1. //Manacher算法
  2. string shortestPalindrome(string s){
  3. if(s.size() <= ) return s;
  4. string ss = "$#";
  5. for(auto c : s) //这样写只需要12ms
  6. ss+=c, ss+='#';
  7. //for(int i = 0; i < s.size(); ++i) //先把字符串转换一下 这样写非常慢 240ms时间都耗在这里了
  8. // ss = ss + s[i] + "#";
  9.  
  10. vector<int> P( * s.size() + , ); //存储以i为中心回文的最大半径
  11. int e = ;
  12. int id = , mx = ; //id为可以管的最远的回文的中心点 mx是id可以管到的最远距离 在回文的后面一个位置
  13. for(int i = ; i < ss.size(); ++i)
  14. {
  15. P[i] = (mx > i) ? min(P[ * id - i], mx - i) : ; //根据之前的信息 获取当前中心点的最短回文长度
  16. while(i + P[i] < ss.size() && ss[i + P[i]] == ss[i - P[i]]) //扩展回文长度 注意不要越界
  17. P[i]++;
  18. if(i + P[i] > mx) //更新最远距离和中心点
  19. {
  20. mx = i + P[i];
  21. id = i;
  22. }
  23. if(i == P[i]) e = i + P[i] - ; //该回文的第一个位置是源字符串的第一个字母 记录回文截至位置
  24. }
  25. e = e / - ; //把回文最长的截至位置转换为在原字符串中的位置
  26. string rs = s.substr(e + , s.size() - e); //在字符串前面补上不够回文的部分
  27. reverse(rs.begin(), rs.end());
  28. return rs + s;
  29. }

大神4ms的代码,其实思路都一样,可大神的代码时间就是短!

  1. string shortestPalindrome(string s) {
  2. int n1=s.length();
  3. string mystr="$";//2*n1+1
  4. int n=*n1+;
  5. for(auto c : s)
  6. mystr+=c, mystr+='$';
  7. int* plen=new int[n];
  8. int pali_len=;
  9. int i, k, mid=, l=-;
  10. for(i=; i<=n/; i++)
  11. {
  12. k=;
  13. if(i<l&&*mid-i>-) k=min(plen[*mid-i], l-i);
  14. while(i-k->-&&i+k+<n&&mystr[i-k-]==mystr[i+k+]) k++;
  15. plen[i]=k;
  16. if(i+k>l) mid=i, l=i+k;
  17. }
  18. for(i=n/; i>-; i--)
  19. if(plen[i]==i) {pali_len=i; break;}
  20. string t=s.substr(i);
  21. reverse(t.begin(), t.end());
  22. return t+s;
  23. }

三个超时的常规思路代码:

  1. bool isPalindrome(string s, int start, int end)
  2. {
  3. while(start <= end)
  4. if(s[start++] != s[end--])
  5. return false;
  6. return true;
  7. }
  8.  
  9. //超时
  10. string shortestPalindrome1(string s) {
  11. if(s.size() <= ) return s;
  12. int e = ;
  13. for(int i = s.size() - ; i >= ; --i) //找到包括第一个字母的最长回文
  14. {
  15. if(isPalindrome(s, , i))
  16. {
  17. e = i;
  18. break;
  19. }
  20. }
  21.  
  22. string rs = s.substr(e + , s.size() - e);
  23. reverse(rs.begin(), rs.end());
  24. return rs + s;
  25. }
  26.  
  27. //超时
  28. string shortestPalindrome2(string s){
  29. if(s.size() <= ) return s;
  30. for(int i = s.size() - ; i >= ; --i) //遍历可能回文的最后一个位置
  31. {
  32. if(s[i] == s[]) //如果第一个字符和最后位置相同
  33. {
  34. int slen = (i + ) / ; //回文一半的长度 奇数个则不要最中间的
  35. string s1 = s.substr(, slen);
  36. string s2 = s.substr(i - slen + , slen);
  37. reverse(s2.begin(), s2.end());
  38. if(s1 == s2) //是回文
  39. {
  40. string rs = s.substr(i + , s.size() - i);
  41. reverse(rs.begin(), rs.end());
  42. return rs + s;
  43. }
  44. }
  45. }
  46. }
  47.  
  48. //超时
  49. string shortestPalindrome3(string s){
  50. if(s.size() <= ) return s;
  51. for(int i = s.size() - ; i >= ; --i) //遍历可能回文的最后一个位置
  52. {
  53. if(s[i] == s[]) //如果第一个字符和最后位置相同
  54. {
  55. int slen = (i + ) / ; //回文一半的长度 奇数个则不要最中间的
  56. string s1 = s.substr(, slen);
  57. string s2 = s.substr(i - slen + , slen);
  58. reverse(s2.begin(), s2.end());
  59. unordered_set<string> uset;
  60. uset.insert(s1);
  61. if(uset.find(s2) != uset.end())
  62. {
  63. string rs = s.substr(i + , s.size() - i);
  64. reverse(rs.begin(), rs.end());
  65. return rs + s;
  66. }
  67. }
  68. }
  69. }

【leetcode】Shortest Palindrome(hard)★的更多相关文章

  1. 【Leetcode】Shortest Palindrome

    Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...

  2. 【leetcode】1278. Palindrome Partitioning III

    题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...

  3. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  4. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  5. 【LeetCode】234. Palindrome Linked List 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  7. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  8. 【题解】【字符串】【Leetcode】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. 【leetcode】9. Palindrome Number

    题目描述: Determine whether an integer is a palindrome. Do this without extra space. 解题分析: ^_^个人觉得这道题没有什 ...

随机推荐

  1. java之String

    一.构造器 package com.string; import java.io.UnsupportedEncodingException; import java.nio.charset.Chars ...

  2. js兼容注意事项--仅供参考

    做BS开发就难免会用到javascript,而每个浏览器对javascript的支持有不同.这就需要我们程序员去兼容他们,不然有些浏览器就无法运行我们的代码.就会造来客户的投诉,如果让BoSS知道了, ...

  3. Why Reflection is slowly?(Trail: The Reflection API)

    反射的使用 反射通常用于在JVM中应用程序运行中需要检查或者修改运行时行为的项目.这是一个相对高级的特性,并且仅仅可以被对深刻理解java原理的开发者使用.这里给出一个警告的意见,反射是一个强大的技术 ...

  4. HDU1102--最小生成树

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. CSU 1116 Kingdoms(枚举最小生成树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 解题报告:一个国家有n个城市,有m条路可以修,修每条路要一定的金币,现在这个国家只 ...

  6. 实战CentOS系统部署Hadoop集群服务

    导读 Hadoop是一个由Apache基金会所开发的分布式系统基础架构,Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS.HDFS有高 ...

  7. 关闭MyEclipse Derby服务

    MyEclipse的Servers视图出现 MyEclipse Derby服务,一直想把它去掉在网上搜索了下,现已解决. 如下,MyEclipse菜单:window-->Preferences- ...

  8. SQL手册

    来自 W3School 的 SQL 快速参考.可以打印它,以备日常使用. SQL 语句 语句 语法 AND / OR SELECT column_name(s)FROM table_nameWHERE ...

  9. STM32F103ZET6 用定时器级联方式输出特定数目的PWM(转载)

    STM32F103ZET6里共有8个定时器,其中高级定时器有TIM1-TIM5.TIM8,共6个.这里需要使用定时器的级联功能,ST的RM0008 REV12的P388和P399页上有说明对于特定的定 ...

  10. c_水程序

    * ** This program reads input lines from the standard input and prints ** each input line, followed ...