【leetcode】Shortest Palindrome(hard)★
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]这样的写法非常耗时!
//Manacher算法
string shortestPalindrome(string s){
if(s.size() <= ) return s;
string ss = "$#";
for(auto c : s) //这样写只需要12ms
ss+=c, ss+='#';
//for(int i = 0; i < s.size(); ++i) //先把字符串转换一下 这样写非常慢 240ms时间都耗在这里了
// ss = ss + s[i] + "#"; vector<int> P( * s.size() + , ); //存储以i为中心回文的最大半径
int e = ;
int id = , mx = ; //id为可以管的最远的回文的中心点 mx是id可以管到的最远距离 在回文的后面一个位置
for(int i = ; i < ss.size(); ++i)
{
P[i] = (mx > i) ? min(P[ * id - i], mx - i) : ; //根据之前的信息 获取当前中心点的最短回文长度
while(i + P[i] < ss.size() && ss[i + P[i]] == ss[i - P[i]]) //扩展回文长度 注意不要越界
P[i]++;
if(i + P[i] > mx) //更新最远距离和中心点
{
mx = i + P[i];
id = i;
}
if(i == P[i]) e = i + P[i] - ; //该回文的第一个位置是源字符串的第一个字母 记录回文截至位置
}
e = e / - ; //把回文最长的截至位置转换为在原字符串中的位置
string rs = s.substr(e + , s.size() - e); //在字符串前面补上不够回文的部分
reverse(rs.begin(), rs.end());
return rs + s;
}
大神4ms的代码,其实思路都一样,可大神的代码时间就是短!
string shortestPalindrome(string s) {
int n1=s.length();
string mystr="$";//2*n1+1
int n=*n1+;
for(auto c : s)
mystr+=c, mystr+='$';
int* plen=new int[n];
int pali_len=;
int i, k, mid=, l=-;
for(i=; i<=n/; i++)
{
k=;
if(i<l&&*mid-i>-) k=min(plen[*mid-i], l-i);
while(i-k->-&&i+k+<n&&mystr[i-k-]==mystr[i+k+]) k++;
plen[i]=k;
if(i+k>l) mid=i, l=i+k;
}
for(i=n/; i>-; i--)
if(plen[i]==i) {pali_len=i; break;}
string t=s.substr(i);
reverse(t.begin(), t.end());
return t+s;
}
三个超时的常规思路代码:
bool isPalindrome(string s, int start, int end)
{
while(start <= end)
if(s[start++] != s[end--])
return false;
return true;
} //超时
string shortestPalindrome1(string s) {
if(s.size() <= ) return s;
int e = ;
for(int i = s.size() - ; i >= ; --i) //找到包括第一个字母的最长回文
{
if(isPalindrome(s, , i))
{
e = i;
break;
}
} string rs = s.substr(e + , s.size() - e);
reverse(rs.begin(), rs.end());
return rs + s;
} //超时
string shortestPalindrome2(string s){
if(s.size() <= ) return s;
for(int i = s.size() - ; i >= ; --i) //遍历可能回文的最后一个位置
{
if(s[i] == s[]) //如果第一个字符和最后位置相同
{
int slen = (i + ) / ; //回文一半的长度 奇数个则不要最中间的
string s1 = s.substr(, slen);
string s2 = s.substr(i - slen + , slen);
reverse(s2.begin(), s2.end());
if(s1 == s2) //是回文
{
string rs = s.substr(i + , s.size() - i);
reverse(rs.begin(), rs.end());
return rs + s;
}
}
}
} //超时
string shortestPalindrome3(string s){
if(s.size() <= ) return s;
for(int i = s.size() - ; i >= ; --i) //遍历可能回文的最后一个位置
{
if(s[i] == s[]) //如果第一个字符和最后位置相同
{
int slen = (i + ) / ; //回文一半的长度 奇数个则不要最中间的
string s1 = s.substr(, slen);
string s2 = s.substr(i - slen + , slen);
reverse(s2.begin(), s2.end());
unordered_set<string> uset;
uset.insert(s1);
if(uset.find(s2) != uset.end())
{
string rs = s.substr(i + , s.size() - i);
reverse(rs.begin(), rs.end());
return rs + s;
}
}
}
}
【leetcode】Shortest Palindrome(hard)★的更多相关文章
- 【Leetcode】Shortest Palindrome
Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...
- 【leetcode】1278. Palindrome Partitioning III
题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- 【LeetCode】234. Palindrome Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【题解】【字符串】【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【leetcode】9. Palindrome Number
题目描述: Determine whether an integer is a palindrome. Do this without extra space. 解题分析: ^_^个人觉得这道题没有什 ...
随机推荐
- wtforms 使用
wtforms是一个表单模板库, 下面以修改密码表单为例简单说明其用法. 我们可以用python代码定义form的基本元素, 比如用户名/邮箱, 并给定各个元素的validation条件. 然后在re ...
- flask 项目的开发经验总结
已经开发了几个flask项目, 是时候总结一下了, 这里涉及到项目源码的组织, 常用的包, 源码示例. =========================需要的 python 包有:========= ...
- ie8及其以下浏览器的document.getElementsByClassName兼容性问题
使用JavaScript访问DOM的一个重大问题是,此过程需要一种通过元素类名称来选择类的类函数,对DOMContentReady,这种类函数的缺失导致开发人员需要自己编写自定义脚本业执行上述任务,许 ...
- SqlServer 还原他服数据库只建立发布却删除不了
本想做测试,从另一台服务器备份数据库还原到本机. 创建了一个发布,却删除不了,提示如下图: 参考论坛:http://bbs.csdn.net/topics/300046417 发现是数据库所有者问题, ...
- R笔记1
gsub format > measurements<-c('3.95*3.99*2.43mm','3*3*5mm','2*2*2mm') > measurements [1] &q ...
- hessian接口参数,子类与父类不能有同名字段解决方法
hessian默认是使用 com.caucho.hessian.io.JavaSerializer 序列化,同名字段子类字段值被赋值两次,最终用父类null值赋给了子类同名字段,解决方法就是 指定序列 ...
- CSS vertical-align 属性
定义和用法 vertical-align 属性设置元素的垂直对齐方式.该属性定义行内元素的基线相对于该元素所在行的基线的垂直对齐
- Swift2.1 语法指南——析构过程
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- php运行出现Call to undefined function curl_init()的解决方法
解决方法如下: 1.在php.ini中找到extension=php_curl.dll,去掉前面的分号;,然后将php.ini拷贝到c:\windows. 2.重启IIS服务,或回收应用程序池即可.
- 淘宝(阿里百川)手机客户端开发日记第十三篇 mysql的连接
首先,我建立了一个包,里面存放了三个类文件,这三个文件是我从网络中找的,经过自己的整理.(我刚才查找想把这三个文件传上去,可能是自己对cnblogs的博客不太熟悉吧,没有找到,我只好粘贴代码了) 三个 ...