Topic:Given A stringS, find the longest palindromic substring inS. Assume that maximum length ofSis, and there exists one unique longest palindromic substring.
Idea: The first way of thinking, is to take an element as the center point to start both sides of the traversal, the longest to traverse the longest palindrome string. In fact, the best way to find back a string of characters is the suffix tree, the suffix tree is the best method.
#include <iostream> #include <string> #include <vector>using namespace std;/* the longest palindrome substring in a string Idea: Start with a character centered on both sides so find the longest substring */int longestpalindromicsub (string& str) {int pre,next,i;int maxlen=0;int pos;for (i= 0;i<str.length (); i++) {Pre =i-1;next = i+1;while (pre>=0 && next<str.length ()) {if (str[pre] = = Str[next ]) {pre--;next++;} Elsebreak;} if (MaxLen < (i-pre) *2+1) {maxlen = Next-pre-1;pos = pre+1;}} Cout<<pos<<endl;return MaxLen;} int main () {string str ("Adoebeoodebedcaaacdabddbdad"); Cout<<longestpalindromicsub (str) <<endl;return 0;}
Longest palindromic Substring--leetcode