LeetCode 214. 最短回文串
2019-03-23 16:59
399 查看
版权声明:转载请注明 https://blog.csdn.net/qq_33831360/article/details/88763944
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
示例 1:
输入: [code]"aacecaaa"输出:"aaacecaaa"[/code]示例 2:
输入: [code]"abcd"输出:"dcbabcd"
和HDU Clairewd’s message类似
其实等价于找最长前缀回文串
(aacecaa)a是前面加a
(a)bcd是前面加dcb
这个长度枚举一下就好了
正着算一次hash,反着算一次,枚举前面添加字符的个数i,hash判断1到s.size()-i是否为回文
复杂度O(n)
[code]class Solution { typedef unsigned long long ULL; const ULL key = 2333; public: vector<ULL> a,b,p; public: ULL gethash(int l,int r,int cas) { return cas ? a[r]-a[l-1]*p[r-l+1]: b[r]-b[l-1]*p[r-l+1]; } public: string shortestPalindrome(string s) { p.push_back(1); a.push_back(0); b.push_back(0); for (int i = 0; i < s.size(); i++) { p.push_back(p[i]*key); a.push_back(a[i]*key+s[i]); b.push_back(b[i]*key+s[s.size()-1-i]); } string per = ""; for (int i = 0; i <= s.size(); i++) if (gethash(1,s.size()-i,1) == gethash(1+i,s.size(),0)) { for (int j = 1; j <= i; j++) per += s[s.size()-j]; return per+s; } s = "Error!"; return s; } };
相关文章推荐
- 【Leetcode】214. Shortest Palindrome 最短回文串
- LeetCode学习(5)Shortest Palindrome(最短回文串)
- [LeetCode] Shortest Palindrome 最短回文串
- LeetCode214.最短回文串
- [LeetCode] Shortest Palindrome 最短回文串
- [LeetCode] 214. Shortest Palindrome 最短回文串
- Leetcode Shortest Palindrome (最短回文串)
- Leetcode刷题28-125.验证回文串(C++)
- leetcode: Pascal's Triangle | Java最短代码实现
- leetcode_125. Valid Palindrome 判断字母数字回文串,大小写转化
- [LeetCode] Shortest Word Distance III 最短单词距离之三
- 【LeetCode】214. Shortest Palindrome
- leetcode 743. Network Delay Time 图最短路径 + Bellman Ford 算法
- [LeetCode] 245. Shortest Word Distance III 最短单词距离 III
- leetcode131.分割回文串
- leetcode 748最短完成字母
- leetcode:Palindrome Partitioning (字符串分割成回文串的所有方式) 【面试算法题】
- leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】
- leetcode 最长回文串
- LeetCode-Python-409. 最长回文串