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)

首先在字符串的每个字符间加上#号。For example: S = “abaaba”, T = “#a#b#a#a#b#a#”。这样所有的回文数都是奇数,以便通过i的对应位置i’获得p[i]
P[i]存储以i为中心的最长回文的长度。For example: 
T = # a # b # a # a # b # a #
P = 0 1 0 3 0 1 6 1 0 3 0 1 0
下面我们说明如何计算P[i]。
假设我们已经处理了C位置(中心位置),它的最长回文数是abcbabcba,L指向它左侧位置,R指向它右侧位置。
现在我们要处理i位置。
if P[ i' ] ≤ R – i,
then P[ i ] ← P[ i' ] 那是因为在L到R范围内,i'的左侧与i的右侧相同,i'的右侧与i的左侧相同,i'左侧与右侧相同 =>i左侧与右侧相同。
else P[ i ] ≥ P[ i' ]. (Which we have to expand past the right edge (R) to find P[ i ].
If the palindrome centered at i does expand past R, we update C to i, (the center of this new palindrome), and extend R to the new palindrome’s right edge.
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)的更多相关文章

  1. leetcode 第五题 Longest Palindromic Substring (java)

    Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...

  2. Leetcode 之Longest Palindromic Substring(30)

    很经典的一道题,最长回文子串,有多种方法. 首先介绍的一种方法是从中间向两边展开.注意区分aba和abba型的回文串:如果当前最长的子串已经当于两边中最长的子串了,则无需再去判断. //从中间向两边展 ...

  3. LeetCode:5. Longest Palindromic Substring(Medium)

    原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/ 1. 题目要求:找出字符串中的最大回文子串 ...

  4. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  5. [LeetCode] Longest Palindromic Substring(manacher algorithm)

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. LeetCode 5 Longest Palindromic Substring(最长子序列)

    题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...

  7. LeetCode OJ:Longest Palindromic Substring(最长的回文字串)

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  8. 21.Longest Palindromic Substring(最长回文子串)

    Level:   Medium 题目描述: Given a string s, find the longest palindromic substring in s. You may assume ...

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

随机推荐

  1. Cmake 编译opengl开源库glfw工程及使用

    使用的是cmake gui进行编译的,路径输入好之后,点configure配置vs版本,这里是vs2013版本,然后如果画面出现红色的 需要再点击一下 Generate 然后直接点open proje ...

  2. CSS3 之转动的风车

    js 可以做动画,但是需要写很多代码:其实css也是可以做动画的,而且比js写的代码还少,理解也相对简单. 这里用到css3 的animation 属性,它配合着 @keyframes 规则来使用,可 ...

  3. sqoop连接SqlServer2012示例

    sqoop import --connect 'jdbc:sqlserver://192.168.xx.xx:1433;username=sa;password=xxxx;database=WindE ...

  4. where后一个条件和多个条件的查询速度

    如果记录中有两个都是 唯一标识的  ,那是都where  and 还是只写一个比较快 ---- 一个快

  5. gitbash上使用tree

    gitbash上使用tree vscode从cmd设置gitbash之后,想在使用windows下的tree命令发现运行不了,有两种解决方案. 1,在gitbash上cmd //c tree,就等同c ...

  6. webkit内核自定义隐藏滚动条

    1,在主页面可以拿到iframe,也可以为iframe注册onload等事件.document.getElementById('iframeId').onload 2,在主页面操作其中的iframe的 ...

  7. 给tkinter文本框添加右键菜单

    给tkinter文本框添加右键菜单 需求:直接右键点击使用tkinter创建的文本框是不会弹出菜单的.我们需要实现右键点击tkinter框架下的Entry对象.Text对象后弹出右键菜单可复制.粘贴和 ...

  8. angularjs 做不到实时脏值查询

    angularjs 做不到脏值查询 ,数据请求过来,不操作其他按钮,请求的值就是展示不出来:(相当于,只有手动触发,angularjs内部才会把脏值查询出来): 解决办法:在请求过来的值旁边加上$sc ...

  9. 修改Http消息的消息头Host

    在 HttpURLConnection 类中直接使用如下代码无法修改Host的值: connection.setRequestProperty("Host", host); 需要在 ...

  10. 导入maven框架报错

    原因pom文件第一行报错X org.apache.maven.archiver.mavenarchiver.getmanifest怎么解决 解决: 原因就是你的maven的配置文件不是最新的 1.he ...