Problem A: string [] palindromic longest substring

Description

        Enter a string, which is obtained palindromic longest substring. Meaning substring is: string segment in the original string of consecutive. Palindrome meaning is: positive, forward looking backwards and look the same. As abba and yyxyy. In determining palindromes, you should ignore all punctuation and spaces, and ignoring the case, but the output should remain intact (in the palindromic sequence of header and trailer do not print extra characters). The length of the input string not more than 5000, and occupies a single line. Longest palindromic sequence to be output, if there are multiple, the starting position of the left most output.

Input

His string, the string length of not more than 5000.

Output

Palindromic longest substring string.

Sample Input

Confuciuss say:Madam,I'm Adam.

Sample Output

Madam,I'm Adam

HINT

Sample Description: Madam, I'm Adam remove spaces, commas, apostrophes, ignoring case is MADAMIMADAM, it is a palindrome.

Algorithm Analysis I:

First solve the problem, "ignore punctuation, output into the judgment but Yaoan as is"? A simple method can be used: pretreatment. Construct a new string that does not contain the original punctuation, and all characters capitalized (the way to solve the problem of capitalization). Used functions:

(1) the isalpha (c) is used to check whether the letter c, if letters, returns 1; 0 otherwise.

(2) isdigit (c) c is used to check whether the number (0 to 9), if a number, the return 1; 0 otherwise.

(3) toupper (c) used to convert to uppercase characters c, c corresponding returns uppercase.

(4) tolower (c) c is used to convert the lowercase characters, lowercase return corresponding c.

 

Let's enumerate the start and end palindrome string, and then determine whether it really is a palindrome string.

int max=0;

for(i = 0; i < m; i++)

   for(j = i; j < m; j++)

       if(s[i..j]是回文串 && j-i+1 > max) max = j-i+1;

"Current maximum" variable max, which stores the length of the palindrome found so far longest substring. If the train s i-th character to the j-th character (referred to as s [i..j]) is a palindromic sequence, the length is checked whether j-i + 1 exceeds the max.

 

Determining whether s [i..j] The method of palindromic sequence is as follows:

int ok = 1;

for(k = i; k <= j; k++)

    if(s[k] != s[i+j-k])   ok = 0;

s [k] is "symmetrical" position s [i + jk], as long as a comparison fails, the flag should be set to 0 the variable ok.

 

 

The final question: is output.

    Since the max value when evaluated, I do not know s [i] and s [j] in the original position of the string in buf. Therefore, it is necessary to increase an array p, save s [i] in buf position by p [i]. Obtained in the pretreatment, and then update the max while the p [i] and p [j] and saved to x y, the final output buf [x] to all characters buf [y] in.

Inadequate: When entering a long string, easy to overtime, because the enumeration palindrome start and end of the cycle too.

 

 

Algorithm Analysis II: enumeration of the palindromic sequence of "neutral" position i, and then continue to expand out until there is a different character. Tip: odd and even length of treatment is not the same.

 

The final AC Code:

#include <bits/stdc++.h>
#include <cstring>
using namespace std;
const int maxn=5005;
int dp[maxn][maxn], hashTable[maxn];
int main(){
    bool flag;
    int i, j, L, low, high;
    string s, st;
    while(getline(cin, s)){
        st = "";
        memset(hashTable, 0, sizeof(hashTable));
        memset(dp, 0, sizeof(dp)); //记得初始化 
        for(i=0; i<s.size(); i++){
            if(isalpha(s[i])){
                st += toupper(s[i]);
                hashTable [I] = . 1 ; // show character 
            } the else  IF (isdigit (S [I])) {
                st += s[i];
                hashTable[i] = 1; 
            }
        }
        for(i=0; i<st.size(); i++) dp[i][i] = 1; //边界
        low = 0;
        high = 0;
        for(L=2; L<=st.size(); L++){
            flag = true;
            for(i=0; i+L-1<st.size(); i++){
                J = I L + - . 1 ;
                 // noted here that when bit when L == 2, DP [I +. 1] [-J. 1] == 0 
                IF (ST [I] == ST [J] && (DP [I + . 1 ] [J- . 1 ] == . 1 || L == 2 )) {
                    DP [I] [J] = . 1 ;
                     IF (In Flag) { // only the first string obtained palindrome length L is updated 
                        Low = I;
                        high = j;
                        In Flag = to false ; // denoted by L time has been updated 
                    }
                }
            }
        }
        for(i=0, j=0; i<s.size()&&j<low; i++) if(hashTable[i] == 1) j++; //找到下端点 
        while(i<s.size() && hashTable[i]==0) i++; //去除下端点前的其它字符 
        for( ; i<s.size()&&j<=high; i++){
            the printf ( " % C " , S [I]);
             IF (hashTable [I] == . 1 ) J ++; // count, to stop the endpoint 
        }
        printf("\n");
    }
    return 0;
}

Summary: originally did not see HINT, and later found the same ideas and HINT of their first ideas. This question, just on the basis of "algorithm notes" on that question added some conditions, therefore special attention when writing code variables do wrong!

Guess you like

Origin www.cnblogs.com/heyour/p/12499886.html