Longest palindromic Substring--leetcode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.