题目链接

  题目要求: 

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

  这道题用暴力破解是无法通过LeetCode上的测试的。

  1. 法一(暴力破解)

  超过时间!!!!

 int findCenter(string s)
{
int center = ;
int szS = s.size();
for (int i = ; i < szS; i++)
{
int k = ;
while (k <= i && k < szS - i)
{
if (s[i - k] != s[i + k])
break;
k++;
}
if (i - k == - && i > center)
center = i;
}
return center;
} string char2String(char c)
{
stringstream ss;
string s;
ss << c;
ss >> s;
return s;
} string shortestPalindrome(string s) {
// clear spaces
if (s.front() == ' ')
s.erase(, );
if (s.back() == ' ')
s.pop_back(); string ret;
int szS = s.size();
if (szS == )
ret = "";
else if (szS == )
{
s += s;
ret = s;
}
else
{
ret = s;
int center = findCenter(s);
int left = center;
int right = center + left + ;
if (szS - center - > center && s[center] == s[center + ])
{
right = szS;
}
for (int i = right; i < szS; i++)
{
ret.insert(, char2String(s[i]));
}
} return ret;
}

  2. 法二(暴力破解)

  法二思路来源:How to get the shortest palindrome of a string。重要语句摘录如下:

  Just append the reverse of initial substrings of the string, from shortest to longest, to the string until you have a palindrome. e.g., for "acbab", try appending "a" which yields "acbaba", which is not a palindrome, then try appending "ac" reversed, yielding "acbabca" which is a palindrome.

  法二相对法一来说更加简单容易理解,但同样超过时间!!!!

 bool isPalindrome(string  s)
{
int szS = s.size();
for (int i = ; i < szS / ; i++)
{
if (s[i] != s[szS - i - ])
return false;
}
return true;
} string shortestPalindrome(string s)
{
// clear spaces
if (s.front() == ' ')
s.erase(, );
if (s.back() == ' ')
s.pop_back();
//
string ret;
int szS = s.size();
if (szS == )
{
ret = "";
}
else if (szS == )
{
s += s;
ret = s;
}
else if (isPalindrome(s))
{
ret = s;
}
else
{
for (int i = ; i < szS; i++)
{
string tmpStr = s;
for (int j = szS - i - ; j < szS; j++)
tmpStr.insert(tmpStr.begin(), s[j]); if (isPalindrome(tmpStr))
{
ret = tmpStr;
break;
}
}
}
return ret;
}

  看来只能另寻他法了。。。。

  3. 法三

  法三参考自C++ 8 ms KMP-based O(n) time & O(n) memory solution,利用了KMP的思想。具体程序如下:

 class Solution {
public:
string shortestPalindrome(string s)
{
string rev_s(s);
reverse(rev_s.begin(), rev_s.end());
string combine = s + "#" + rev_s; // 防止在匹配的时候,不从‘#’后开始 int sz = combine.size();
vector<int> match(sz, );
int k = ;
for (int i = ; i < sz; i++) {
while (k > && combine[i] != combine[k])
k = match[k - ]; if (combine[i] == combine[k])
k = k + ; match[i] = k;
} return rev_s.substr(, s.size() - match[sz - ]) + s;
}
};

  上边程序可以解释如下:

  对构造的字符串combine进行匹配操作后,得到如下结果:

  

  匹配部分也就是s和rev_s重复的部分,而不匹配的部分就是它们不一样的部分。接下来的字符串拼接操作就是:

  

  拼接完成后即是最终的结果。

LeetCode之“字符串”:最短回文子串的更多相关文章

  1. LeetCode:最长回文子串【5】

    LeetCode:最长回文子串[5] 题目描述 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: ...

  2. 【LeetCode】最长回文子串【动态规划或中心扩展】

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad"输出: "bab"注意: " ...

  3. Java实现 LeetCode 5 最长回文子串

    5. 最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab&quo ...

  4. leetcode python最长回文子串

    回文的意思是正着念和倒着念一样,如:上海自来水来自海上,雾锁山头山锁雾,天连水尾水连天 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: & ...

  5. [LeetCode] 5. 最长回文子串

    题目链接:https://leetcode-cn.com/problems/longest-palindromic-substring/ 题目描述: 给定一个字符串 s,找到 s 中最长的回文子串.你 ...

  6. LeetCode 05 最长回文子串

    题目 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: ...

  7. [LeetCode] 5. 最长回文子串 ☆☆☆(最长子串、动态规划)

    最长回文子串 (动态规划法.中心扩展算法) https://leetcode-cn.com/problems/longest-palindromic-substring/solution/xiang- ...

  8. 【Leetcode】最长回文子串

    启发 1)做题前一定要读懂题目 在本题中,首先要清楚地定义回文子串的概念,然后才能设计算法查找它. 如中心扩散法,其主要思想在于找到一个回文子串的定义——两侧互为镜像.进一步分为奇数长度和偶数长度进行 ...

  9. 【LeetCode】最长回文子串-动态规划法

    [问题]给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 : 输入: "babad" 输出: "bab" 注意: ...

  10. 【LeetCode】最长回文子串-中心扩展法

    [问题]给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 : 输入: "babad" 输出: "bab" 注意: ...

随机推荐

  1. 关于bootstrap在IE8下不能支持自适应的问题

    说到这个问题,我就想吐槽下IE了,开发这么多版本,每个版本都有一些这样那样的问题不支持,别的正常的浏览器咋都能支持呢?真是垃圾浏览器!!!! 说归说,但是IE现在用的人多啊,怎么办?这个问题还是得解决 ...

  2. 禁止通过网页URL访问文件夹 asp.net

    我们可以通过如下的两种办法,禁止用户通过浏览器的URL地址直接访问网站服务器的文件夹. 一.通过类和配置文件限制 ①NET C#代码 新建一个类,继承IHttpHandler using System ...

  3. Scala actor的使用

    Actor 为什么需要Actor? Actor的本质即万物皆Actor, Actor之间只有发送消息这一种通信方式.例如,无论是管理员让工作者干活,还是工作者把成果交还给管理员,它们之间也要通过发送消 ...

  4. shell 数据流重定向操作符总结

    最近看了鸟哥私房菜关于shell数据流重定向的内容,总结一下. 操作符: 1.标准输入(stdin):代码为0,符号:< 或者<< 2.标准输出(stdout):代码为1,符号:&g ...

  5. C语言函数--atoi

          在Java语言中,由于面向对象的思想,它对基本数据类型也进行了相应的封装,例如 int 就封装成了 Integer 类,这无疑会使我们的操作方便了许多,例如,有一个字符串,我想把它转换为i ...

  6. (NO.00005)iOS实现炸弹人游戏(七):游戏数据的序列化表示

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 用plist列表文件来表示游戏数据 因为在这个炸弹人游戏中有很多 ...

  7. Fragment详解-android学习之旅(四十八)

    Fragment的设计哲学 Fragment的继承体系 Fragment的开发 大部分都会继承如下的三个方法 Fragment与Activity的通信 Fragment与Activity交互信息 Fr ...

  8. findViewById中NullPointerException的错误

    最近在弄一个对话框的登录时,发现一个总是报NullPointerException的错误,折腾了两小时,一直没有发现细小的区别..先上图,一边说明原因 首先是 Activity类中定义的findVie ...

  9. 根据CSS的class选择DOM

    // 网上参考的,自己修改了一部分 // 代码如下,纯JS,要求浏览器支持 getElementsByClassName 方法 <script type="text/javascrip ...

  10. Uva - 804 - Petri Net Simulation

    Input: petri.in A Petri net is a computational model used to illustrate concurrent activity. Each Pe ...