psd面试(最长回文子串 == 最长公共子序列 lcs) 另附最长回文串算法

https://www.nowcoder.com/acm/contest/90/D

get了两个函数tolower() , toupper()

回顾lcs的写法

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000;
int c[maxn][maxn];
int main()
{
    string st1,st2;
    while(cin>>st1)
    {
        st2 = st1;
        reverse(st2.begin(),st2.end());
        st1 = " "+st1;
        st2 = " "+st2;
        //printf("%c",tolower('!'));
        //cout<<st1<<endl;
        /*cout<<st2<<endl;
        cout << "Hello world!" << endl;*/
        int n = st1.size();

        for(int i=1; i<=n; i++)
        {
            c[i][0]=0;
            c[0][i]=0;
        }
        int m = 0;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(tolower(st1[i]) == tolower(st2[j]))
                {
                    c[i][j] = c[i-1][j-1]+1;
                }
                else c[i][j] = max(c[i-1][j], c[i][j-1]);
                m = max(m,c[i][j]);
            }

        }
        //cout<<m-1<<endl;
        cout<<n-m<<endl;
    }

    return 0;
}

另外,求最大回文串(连续)的o(n)算法

马拉车算法

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_BUF 1024

#define MIN(a, b) ((a)<(b)?(a):(b))

int main(int argc, char *argv[])
{
    char ori_str[MAX_BUF];

    while(NULL != gets(ori_str))
    {
    int i = 0;
    int ori_strlen = 0;

    while('\0' != ori_str[ori_strlen++]);

    const int trans_strlen = 2 * (ori_strlen - 1);
    char* str = (char*)malloc(trans_strlen + 1);

    if(NULL == str)
        continue;

    //init string str[]
    str[0] = '$';
    str[1] = ori_str[0];
    for(i = 1; i < ori_strlen; i++)
    {
        str[2 * i] = '#';
        str[2 * i + 1] = ori_str[i];
    }
    str[trans_strlen] = '\0';


    int* P = (int*) malloc(trans_strlen * sizeof(int));
    if(NULL == P)
        continue;
    P[0] = 0;

    int id = 0;
    int mx = 0;

    //update P[]
    for(i = 1; i < strlen(str); i++)
    {
        if(i < mx)
            P[i] = MIN(P[2 * id - i], mx - i);
        else
            P[i] = 0;

        while(str[i + P[i] + 1] == str[i - P[i] - 1])
            P[i]++;

        if (P[i] + i > mx)
        {
            id = i;
            mx = P[i] + id;
        }
    }

    //find the longest palindromic string
    int max_len = 0;
    int max_index = 0;

    for(i = 1; i < trans_strlen; i++)
    {
      if(P[i] > max_len)
      {
        max_len = P[i];
        max_index = i;
      }
    }

    char* longest_palindrome = (char*)malloc(max_len + 1);

    if(NULL == longest_palindrome)
      continue;

    if('#' == str[max_index - max_len])
      max_len--;

    if(0 == max_len)
    {
      printf("There is no palindromic\n");
      continue;
    }
      int longest_palindrome_pos = (max_index - max_len)/2;

    for (i = 0; i < max_len + 1; i++)
        longest_palindrome[i] = ori_str[longest_palindrome_pos++];
    longest_palindrome[++max_len] = '\0';
    printf("The length of the longest palindromic substring is: %d\n", max_len);
    printf("The longest palindromic substring: %s\n", longest_palindrome);

    free(str);
    free(P);
    free(longest_palindrome);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37591656/article/details/79713704