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
  • 相关阅读:
    BZOJ 1221 [HNOI2001] 软件开发 费用流_建模
    BZOJ 1180 / 2843 LCT模板题 + 双倍经验
    bzoj 4372: 烁烁的游戏 动态点分治+树链剖分+线段树
    bzoj 3730: 震波 动态点分治+树链剖分+线段树
    luogu P2634 [国家集训队]聪聪可可 点分治
    bzoj 1468: Tree 点分治
    bzoj 1296: [SCOI2009]粉刷匠 动态规划
    bzoj 1293: [SCOI2009]生日礼物 问题转化 + 性质分析 + 滚动数组优化
    BZOJ 1042: [HAOI2008]硬币购物 容斥原理+背包
    matplotlib模块
  • 原文地址:https://www.cnblogs.com/x512149882/p/4738744.html
Copyright © 2011-2022 走看看