5. Longest Palindromic Substring (DP)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
思路:如果不用动态规划,在两个for循环的情况下,还得依次比较i,j间的每个字符,O(n3)。使用动态规划,O(n2)
char* longestPalindrome(char* s) {
int n = strlen(s);
int max = ;
int pStart = ; bool flag[n][n];
for(int i = ; i < n; i++){
flag[i][i] = true;
for(int j = i+; j < n; j++){
flag[i][j] = false;
}
} for(int j = ; j < n; j++){ //when iterate j, we should already iterated j-1, 可以理解成j之前已排序好=>用插入排序的顺序遍历
for(int i = ; i < j; i++){
if(s[i]==s[j]){
flag[i][j] = (j==i+)?true:flag[i+][j-]; if(flag[i][j] && j-i+ > max){ max = j-i+;
pStart = i;
}
}
}
} s[pStart+max]='\0';
return &s[pStart];
}
方法II:KMP+动态规划,时间复杂度在最好情况下达到O(n)

char* preProcess(char* s) {
int n = strlen(s);
if (n == ) return "^$";
char* ret = malloc(sizeof(char)*(n*+));
char* pRet = ret;
*pRet++ = '^'; //开始符^
for (int i = ; i < n; i++){
*pRet++ = '#';
*pRet++ = s[i];
}
*pRet++ = '#';
*pRet = '$';//结束符$
return ret;
} char* longestPalindrome(char* s) {
char* T = preProcess(s);
int n = strlen(T);
int P[n];
int C = , R = ;
char* ret;
for (int i = ; i < n-; i++) {
int i_mirror = *C-i; // equals to i_mirror = C - (i-C) //if p[i_mirror] < R-i: set p[i] to p[i_mirror]
if(R>i){
if(P[i_mirror] <= R-i){
P[i] = P[i_mirror];
}
else P[i] = R-i;
}
else P[i] = ; //else: Attempt to expand palindrome centered at i
while (T[i + + P[i]] == T[i - - P[i]]) //因为有哨兵^$所以不用担心越界; +1, -1检查下一个元素是否相等,若相等,扩大p[i]
P[i]++; //if the palindrome centered at i does expand past R
if (i + P[i] > R) {
C = i;
R = i + P[i];
}
} // Find the maximum element in P.
int maxLen = ;
int centerIndex = ;
for (int i = ; i < n-; i++) {
if (P[i] > maxLen) {
maxLen = P[i];
centerIndex = i;
}
} ret = malloc(sizeof(char)*maxLen+);
strncpy(ret, s+(centerIndex - - maxLen)/, maxLen);
ret[maxLen] = '\0';
return ret;
}
5. Longest Palindromic Substring (DP)的更多相关文章
- leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
- Leetcode 之Longest Palindromic Substring(30)
很经典的一道题,最长回文子串,有多种方法. 首先介绍的一种方法是从中间向两边展开.注意区分aba和abba型的回文串:如果当前最长的子串已经当于两边中最长的子串了,则无需再去判断. //从中间向两边展 ...
- LeetCode:5. Longest Palindromic Substring(Medium)
原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/ 1. 题目要求:找出字符串中的最大回文子串 ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- [LeetCode] Longest Palindromic Substring(manacher algorithm)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- LeetCode 5 Longest Palindromic Substring(最长子序列)
题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...
- LeetCode OJ:Longest Palindromic Substring(最长的回文字串)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 21.Longest Palindromic Substring(最长回文子串)
Level: Medium 题目描述: Given a string s, find the longest palindromic substring in s. You may assume ...
- 5.Longest Palindromic Substring (String; DP, KMP)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
随机推荐
- kubernets之endpoints
注:本文整理自网络 endpoint endpoint是k8s集群中的一个资源对象,存储在etcd中,用来记录一个service对应的所有pod的访问地址.service配置selector,endp ...
- js window.open隐藏参数提交
1.采用form方式提交 var url = "page/public/exportExcel.jsp"; //create a form var tempForm = docum ...
- leetcode1021
class Solution(object): def removeOuterParentheses(self, S: str) -> str: li = list() bcode = 0 te ...
- 机器学习进阶-光流估计 1.cv2.goodFeaturesToTrack(找出光流估计所需要的角点) 2.cv2.calcOpticalFlowPyrLK(获得光流检测后的角点位置) 3.cv2.add(进行像素点的加和)
1.cv2.goodFeaturesToTrack(old_gray, mask=None, **feature_params) 用于获得光流估计所需要的角点参数说明:old_gray表示输入图片, ...
- leetcode题解 candy
要求的条件是: 1.每个人最少一个糖果. 2.相邻的小朋友,要保证,评分高的比评分低的糖果多. 如果从一侧扫描的话,容易确定的就是递增序列,只要上升1个就够了. 容易出现问题的就是:遇到下降期,或者相 ...
- 修改 计算机名后,修改SQLserver 注册服务器对象的名称,及登陆名
select @@ServerName --查看当前所有数据库服务器名称select * from Sys.SysServers --修改数据库服务器名称sp_dropserver 'old_serv ...
- Linux:客户端的实现
写了一个简单的服务器软件,但是没有写客户端.现在我将客户端实现了,其实昨天已经说了客户端的实现步骤了. 步骤: socket() 初始化 connet()链接 从标准输入读数据fgets() 传数据到 ...
- C++中 int i 与 int &i 注意事项
来源:http://blog.csdn.net/qianchenglenger/article/details/16949689 1.int i 传值,int & i 传引用 int i不会回 ...
- python实现根据目标字符串修改一下行
需求: 根据source和dest两个文件,找出新增的命令行,然后在xml文件中根据命令修改id 输入souce: ADD 100 SUB 200 输入dest: MUL 300 DIV 400 AD ...
- http://sourceforge.net/projects/rtspdirectshow/
如何做一个解析rtsp协议的h264压缩的实时视频流播放器,带保存功能,目前我有rtsp协议的h264压缩后的实时视频流,目前想开发一个客户端,来播放该实时视频流,同时保存为视频文件,目前似乎有方案是 ...