zoukankan      html  css  js  c++  java
  • ACM 回文串&最大公共子序列

    Description

    By definition palindrome is a string which is not changed when reversed. "MADAM" is a nice example of palindrome. It is an easy job to test whether a given string is a palindrome or not. But it may not be so easy to generate a palindrome.

    Here we will make a palindrome generator which will take an input string and return a palindrome. You can easily verify that for a string of length n, no more than (n - 1) characters are required to make it a palindrome. Consider "abcd" and its palindrome "abcdcba" or "abc" and its palindrome"abcba". But life is not so easy for programmers!! We always want optimal cost. And you have to find the minimum number of characters required to make a given string to a palindrome if you are only allowed to insert characters at any position of the string.

    Input

    Input starts with an integer T (≤ 200), denoting the number of test cases.

    Each case contains a string of lowercase letters denoting the string for which we want to generate a palindrome. You may safely assume that the length of the string will be positive and no more than 100.

    Output

    For each case, print the case number and the minimum number of characters required to make string to a palindrome.

    Sample Input

    6

    abcd

    aaaa

    abc

    aab

    abababaabababa

    pqrsabcdpqrs

    Sample Output

    Case 1: 3

    Case 2: 0

    Case 3: 2

    Case 4: 1

    Case 5: 0

    Case 6: 9

    解题思路:

    题目大意是这样的,我们输入一个字符串,然后问我们需要改变几个字符可以使这个字符串变成一个回文字符串。我们将这个字符串存在a数组中,然后将这个字符的逆序存在b数组中,这样我们就得到了两个数组,我们可以把这个题目转化为一个求最大公共子序列的问题了,我们求出这两个数组的最大公共子序列,然后将这个数组的长度减去这个最大公共子序列的长度,就得到了我们需要改变的字符个数。

    程序代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int N=105;
    char a[N],b[N];
    short save[N][N];
    int n;
    int max(int x,int y)
    {
        return x>y?x:y;
    }
    int maxlen(char *str1,char *str2)
    {
        memset(save,0,sizeof(save));
        int n1=strlen(str1),n2=strlen(str2),i,j;
        for(i=0;i<=max(n1,n2);i++)
            save[i][0]=save[0][i]=0;
        for(i=1;i<=n1;i++)
            for(j=1;j<=n2;j++)   //以下从str1[0]和str2[0]开始比较
                save[i][j]=(str1[i-1]==str2[j-1] ? save[i-1][j-1]+1 : max(save[i-1][j],save[i][j-1]));
        return save[n1][n2];
    }
    int main()
    {
        int t,k=0;
        cin>>t;
        while(t--)
        {
            scanf("%s",a);
            int n=strlen(a);
            for(int i=0;i<n;i++)
               b[n-i-1]=a[i];
            b[n]='';
            cout<<"Case "<<++k<<": "<<n-maxlen(a,b)<<endl;
        }
        return 0;
    }
  • 相关阅读:
    linux 时间设置
    linux
    linux 关闭防火墙
    GIS-008-ArcGIS JS API 全图
    GIS-007-Terrain跨域访问
    GIS-006-ArcGIS API 空间关系
    Python 中文乱码
    GIS-005-Dojo & jQuery 事件处理
    GIS-004-Cesium版权信息隐藏
    GIS-003-在线地图下载及应用
  • 原文地址:https://www.cnblogs.com/xinxiangqing/p/4730962.html
Copyright © 2011-2022 走看看