zoukankan      html  css  js  c++  java
  • csu 1102 Palindrome

    (当然,思路是大牛的)找出反转串与原串的最长公共子列(不连续),然后总长度减去这个LCS的长度即可;

    dp,空间优化是显然可以的,但是……先AC了再说。。;

    3WA:题目要求大小写是distinct,没有仔细读题就想当然地把大写转为小写了;

          求c[i][j]时,比较的是x[i-1]与y[j-1];

        将c定义为字符型数组,显然通不过,字符型最大才127,就改为int吧。。

    1MLE:接上,int型的c会严重MLE的,题目要求最大长度为5000,short足够用,再改为short。

    /* LCS 问题 */

    # include <stdio.h>

    # define MAX(a,b) ((a)>(b) ? (a):(b))
    # define MAXN 5005

    char str[MAXN];
    char revs[MAXN];
    short int c[MAXN+1][MAXN+1];

    int LCS(char *x, char *y, int n);

    int main()
    {
    int i, n;

    while (~scanf("%d%s", &n, str))
    {
    for (i = 0; i < n; ++i)
    revs[i] = str[n-1-i];
    printf("%d\n", n - LCS(str, revs, n));
    }

    return 0;
    }

    int LCS(char *x, char *y, int n)
    {
    int i, j;

    for (i = 1; i <= n; ++i)
    c[i][0] = 0;
    for (j = 0; j <= n; ++j)
    c[0][j] = 0;
    for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
    {
    if (x[i-1] == y[j-1])
    c[i][j] = c[i-1][j-1] + 1;
    else c[i][j] = MAX(c[i-1][j], c[i][j-1]);
    }
    return c[n][n];
    }



  • 相关阅读:
    从进入这里,没有写过什么文章,现在开始吧
    24)
    23)
    22)
    21)
    20)
    19)
    18)
    17)
    16)
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2412695.html
Copyright © 2011-2022 走看看