zoukankan      html  css  js  c++  java
  • Light OJ 1033 .5THproblem A. DP 最长回文子序列

    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

     分析:
    dp[i][j]表示从i到j最少要添加的字符个数
    if(a[i]==a[j])
                    dp[i][j]=dp[i+1][j-1];  //dp等于上一个状态的dp
                else
                    dp[i][j]=min(dp[i+1][j],dp[i][j-1])+1;  //取两种划分的最小值
    代码及分析如下:
     1 //dp[i][j]表示从i到j最少要添加的字符个数
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<string>
     6 #include<algorithm>
     7 using namespace std;
     8 int dp[105][105];
     9 string a;
    10 int min(int x,int y)
    11 {
    12     if(x<=y)
    13         return x;
    14     else
    15         return y;
    16 }
    17 
    18 int main()
    19 {
    20     int t,len,i,j,Case=1;
    21     cin>>t;
    22     while(t--)
    23     {
    24         cin>>a;
    25         len=a.size();
    26         memset(dp,0,sizeof(dp)); 
    27         for(i=len-2;i>=0;i--)    //注意i从倒数第二个位置开始
    28             for(j=i+1;j<=len-1;j++)  //j<=len-1而不是j<=len-2
    29            {
    30             if(a[i]==a[j])
    31                 dp[i][j]=dp[i+1][j-1];  //dp等于上一个状态的dp
    32             else
    33                 dp[i][j]=min(dp[i+1][j],dp[i][j-1])+1;  //取两种划分的最小值
    34 
    35            }
    36         printf("Case %d: %d
    ",Case++,dp[0][len-1]);
    37     }
    38 
    39     return 0;
    40 }  
    View Code
  • 相关阅读:
    关于对defer的理解.
    JAVA文件操作类和文件夹的操作
    跟我学XSL(一)
    .NET Remoting程序开发入门篇(一)
    jboss配置入门(一)
    SQL2000 关于 Java JDBC 驱动的安装和设定
    XSL基础教程(三)
    利用WSCF进行契约先行的Web Services开发
    Log4Net使用指南
    java存储过程调用(sqlsever数据库)
  • 原文地址:https://www.cnblogs.com/x512149882/p/4738744.html
Copyright © 2011-2022 走看看