Longest echo substring Algorithm

Source: Internet
Author: User

Longest echo substring Algorithm
#1032: Maximum length of the reply substring time limit: Ms single point of time limit: Ms memory limit: 64 MB

Description

Xiao Hi and Xiao Ho are good friends. They were born in the information society and have a great interest in programming. They agreed to help each other and move forward along the learning path of programming.

On this day, they met a series of strings, so xiao Hi raised the classic question to Xiao Ho: "Xiao Ho, can you find the longest substring of each of these strings?"

Little Ho asked strangely: "What is the longest reply string ?"

Xiao Hi replied: "The continuous section in a string is the substring of this string, and the return string refers to the same string as the 12421 string that is read from the back and forth and read from the back and forth, so the longest reply substring means the longest substring in this string as the return substring ~"

Little Ho said: "This is the case! How can I get these strings? How can I tell you the longest response substring I calculated?

Xiao Hi said with a smile: "This is very easy. You only need to write a program to read an integer N (N <= 30) from the standard input ), represents the number of strings I have given you, and the next step is the N strings (String Length <= 10 ^ 6) that I want to give you. If you want to tell me your answer, you just need to output the maximum length of the substring you calculated to the standard output in the order I gave you! This is an example ."


Sample Input
3abababaaaaabaaacacdas
Sample output
753

Question Analysis:

The longest echo substring, as its name implies, is a substring of a string that meets the requirements of the ECHO string, such as abba and aba.

Ideas:

First consider the return of an odd number of characters, as to the even number, and then raise it later.

1. enumerate the length of each string-centered echo substring, for example, aabac, with the first to fifth characters as the centers respectively, the return substrings with an odd enumerated length. The maximum length of the input string.

2. For aaaaaaaaaaab, this type of string may be slightly optimized during enumeration because it contains many identical characters. The specific optimization ideas are as follows:


In, f (I) represents the longest substring centered on character I. Based on the above information, we can infer that A [5] = A [3], A [3] = A [1], A [1] = A [7], so A [5] = A [7], so f (6)> = 3. Therefore, when enumerative, you only need to consider A [4] = A [8]?, If it is equal to continuing to judge A [3] = A [9]?, If not, you can record the longest substring as the value of f (6. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Export + export + CgoKPHA + export + IGo8L3A + CgoKPHA + v8nS1L + export/export + 9 PDtMvQxc + ivLS/ybTvtb3P + export/export + CgoKPHA + PGJyPgo8L3A + CgoKPHA + export + m /9 qOsu/mxvsdxzayjuw.vcd4kcgo8cd48aw1nihnyyz0 = "http://www.2cto.com/uploadfile/Collfiles/20150116/20150116090613198.png" alt = "\">


The code is appended for analysis:

#include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        using namespace std;#define N 1000010class Solution {public:    char buf[N];    int f[N];    void solve() {        int kase;        scanf("%d", &kase);        while (kase--) {            scanf("%s", &buf);            int longestLength = longestPalindrome(buf);            printf("%d\n", longestLength);        }    }    int longestPalindrome(char buf[]) {        int n = strlen(buf);        int j = 0, i, k;        // in case the length of palindrome is odd        f[0] = 1;        int result = 1;        for (i = 1; i < n; i++) {            f[i] = min(f[2*j-i] + 2*i, f[j]+2*j) - 2*i;            if (f[i] < 0) f[i] = 1;            for (int left = i - f[i]/2 - 1, right = i + f[i]/2 + 1; left >= 0, right < n; left--, right++) {                if (buf[left] == buf[right]) f[i] += 2;                else break;            }            if (f[i] + 2*i > f[j] + 2*j) j = i;            result = max(result, f[i]);        }        // in case the length of palindrome is even        f[0] = 0; j = 0;        for (i = 0; i < n-1; i++) {            f[i] = min(f[2*j-i], f[j]-2*i+2*j);            if (f[i] < 0) f[i] = 0;            for (int left = i - f[i]/2, right = i + 1 + f[i]/2; left >= 0 , right < n; left--, right++) {                if (buf[left] == buf[right]) f[i] += 2;                else break;            }            if (f[i] + 2*i > f[j] + 2*j) j = i;            result = max(result, f[i]);        }        return result;    }};int main() {    Solution solution;    solution.solve();    return 0;}
       
      
     
    
   


Related Article

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.