【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. 解题分析: ^_^个人觉得这道题没有什 ...
随机推荐
- [设计模式] javascript 之 迭代子模式
迭代子模式:定义 迭代子模式,又称游标模式,是一种用于对聚集进行顺序访问规则的模式,是一种行为模式:它用于提供对聚集对象的一种统一的访问接口,使客户能够在不了解聚集对象内部结构的情况对聚集对象进行访问 ...
- HTTP报文详解
二.HTTP请求首部字段 1 Accept 2 Accept-Charset 3 Accept-Encoding 4 Accept-Language 5 Authorization 6
- 有利于SEO优化的DIV+CSS的命名规则小结
可以先去这里温习一下CSS和HTML的知识!DIV+CSS规范命名大全集合 CSS开发技巧整理 一.CSS文件及样式命名 1.CSS文件命名规范 全局样式:global.css/master.css ...
- CSS浮动属性Float介绍
#cnblogs_post_body h6 {font-size: 16px;font-weight: bold;} 什么是CSS Float? float 是 css 的定位属性.在传统的印刷布局中 ...
- Excel 使用宏批量修改单元格内指定文字为红字
-> step 1:新建宏,进入编辑,使用如下代码: Sub Ss()Dim c As RangeFor Each c In ActiveSheet.UsedRange i = 1 While ...
- Java多线程初学者指南(7):向线程传递数据的三种方法
在传统的同步开发模式下,当我们调用一个函数时,通过这个函数的参数将数据传入,并通过这个函数的返回值来返回最终的计算结果.但在多线程的异步开发模式下,数据的传递和返回和同步开发模式有很大的区别.由于线程 ...
- PHP基础Mysql扩展库
mysql扩展库操作步骤如下: 1.连接数据库 2.选择数据库 3.设置操作编码 4.发送指令sql,并返回结果集 ddl:数据定义语句 dml:数据操作语句 dql:数据查询 ...
- Java VisualVM使用:堆OOM
背景 近期遇到了一个java.lang.OutOfMemoryError: Java heap space的问题,排除了堆设置过小的问题,代码走查没有头绪,所以使用VisualVM工具分析堆内存情况. ...
- 关于百度地图api测距显示NaN的解决方案
因为随着百度地图的api的升级,测距的函数以及语句都发生的一定变化. 在调用api测距的时候通常我们使用的是语句map.getDistance(marker1,marker2); 但为什么这么简单的测 ...
- Extjs 学习笔记1
学习笔记 目 录 1 ExtJs 4 1.1 常见错误处理 4 1.1.1 多个js文件中有相同的控件,切换时无法正常显示 4 1.1.2 Store的使用方法 4 1.1.3 gridPanel ...