标签:put 展开 code 上进 微软 string i+1 one 算法
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
寻找最长回文子串
Example:
Input: "babad" Output: "bab" Note: "aba" is also a valid answer.
Example:
Input: "cbbd" Output: "bb"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class Solution { public: string longestPalindrome(string s) { int max = 0, maxi = 0, maxc = 0; for (int i = 0; i< s.size(); ++i) { for (int j = 1; i + j - 1< s.size(); ++j) { if (isPalindromic(s.substr(i, j))) if (j> max) { max = j; maxi = i; maxc = j; } } } return s.substr(maxi, maxc); } bool isPalindromic(string s) { for (int i = 0; i < s.size() / 2; ++i) { if (s[i] != s[s.size() - 1- i]) return false ; } return true ; } }; |
定义 P[ i, j ] ← 如果子串Si … Sj 是一个回文,那么该项为true, 否则为false
P[ i, j ] 为 true ← ( P[ i+1, j-1 ]为true,并且Si = Sj )
P[ i, i ] 一定是true
P[ i, i+1 ] 为true ← ( Si = Si+1 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | string longestPalindromeDP(string s) { int n = s.length(); int longestBegin = 0; int maxLen = 1; bool table[1000][1000] = { false }; for ( int i = 0; i < n; i++) { table[i][i] = true ; } for ( int i = 0; i < n-1; i++) { if (s[i] == s[i+1]) { table[i][i+1] = true ; longestBegin = i; maxLen = 2; } } for ( int len = 3; len <= n; len++) { //对长度为3,4,5……的子串进行遍历 for ( int i = 0; i < n-len+1; i++) { //以len为窗口,在s上进行平移,判断是否符合递推条件 int j = i+len-1; if (s[i] == s[j] && table[i+1][j-1]) { table[i][j] = true ; longestBegin = i; maxLen = len; } } } return s.substr(longestBegin, maxLen); } |
第一次循环以后,table值如下
第二次循环以后,table值如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Solution { public : string longestPalindrome(string s) { int len = s.size(); if (len == 0) return "" ; string longest; for ( int i = 0; i < len; ++i){ string s1 = expand(s,i,i); if (s1.size() > longest.size()) longest = s1; string s2 = expand(s,i,i+1); if (s2.size() > longest.size()) longest = s2; } return longest; } string expand(string s, int l, int r){ int len = s.size(); while (l>=0 && r < len && s[l] == s[r]){ --l; ++r; } return s.substr(l+1, r - l -1); } }; |
初始时,i=0 (奇 代表奇数长子串,偶 代表偶数长子串)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | // Transform S into T. // For example, S = "abba", T = "^#a#b#b#a#$". // ^ and $ signs are sentinels appended to each end to avoid bounds checking string preProcess(string s) { int n = s.length(); if (n == 0) return "^$" ; string ret = "^" ; for ( int i = 0; i < n; i++) ret += "#" + s.substr(i, 1); ret += "#$" ; return ret; } string longestPalindrome(string s) { string T = preProcess(s); int n = T.length(); int *P = new int [n]; int C = 0, R = 0; for ( int i = 1; i < n-1; i++) { int i_mirror = 2*C-i; // equals to i‘ = C - (i-C) P[i] = (R > i) ? min(R-i, P[i_mirror]) : 0; // Attempt to expand palindrome centered at i if (i + P[i] >= R) // There‘s no need to expand palindrome when i + P[i] < R while (T[i + 1 + P[i]] == T[i - 1 - P[i]]) P[i]++; // If palindrome centered at i expand past R, // adjust center based on expanded palindrome. if (i + P[i] > R) { C = i; R = i + P[i]; } } // Find the maximum element in P. int maxLen = 0; int centerIndex = 0; for ( int i = 1; i < n-1; i++) { if (P[i] > maxLen) { maxLen = P[i]; centerIndex = i; } } delete [] P; return s.substr((centerIndex - 1 - maxLen)/2, maxLen); } |
R
C
i
T: ^ # b # a # b # c # b # a # b # c # b # a # c # c # b # a # $
P:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
R
C
i
T: ^ # b # a # b # c # b # a # b # c # b # a # c # c # b # a # $
P: 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
R
C
i
T: ^ # b # a # b # c # b # a # b # c # b # a # c # c # b # a # $
P: 0 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
R
C
i
T: ^ # b # a # b # c # b # a # b # c # b # a # c # c # b # a # $
P: 0 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
R
C
i
T: ^ # b # a # b # c # b # a # b # c # b # a # c # c # b # a # $
P: 0 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
R
C
i
T: ^ # b # a # b # c # b # a # b # c # b # a # c # c # b # a # $
P: 0 1 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
R
C
i
T: ^ # b # a # b # c # b # a # b # c # b # a # c # c # b # a # $
P: 0 1 0 3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
R
C
i‘ i
T: ^ # b # a # b # c # b # a # b # c # b # a # c # c # b # a # $
P: 0 1 0 3 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
R
C
i‘ i
T: ^ # b # a # b # c # b # a # b # c # b # a # c # c # b # a # $
P: 0 1 0 3 0 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
R
C
i
T: ^ # b # a # b # c # b # a # b # c # b # a # c # c # b # a # $
P: 0 1 0 3 0 1 0 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
R
C
i‘ i
T: ^ # b # a # b # c # b # a # b # c # b # a # c # c # b # a # $
P: 0 1 0 3 0 1 0 7 0 1 0 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
5. Longest Palindromic Substring
标签:put 展开 code 上进 微软 string i+1 one 算法
原文地址:http://www.cnblogs.com/zhxshseu/p/94882e9ef7cddcc48293c9e617c81284.html