• 1033


    1033 - Generating Palindromes
    Time Limit: 2 second(s) Memory Limit: 32 MB

    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

    Output for Sample Input

    6

    abcd

    aaaa

    abc

    aab

    abababaabababa

    pqrsabcdpqrs

    Case 1: 3

    Case 2: 0

    Case 3: 2

    Case 4: 1

    Case 5: 0

    Case 6: 9


    PROBLEM SETTER: MD. KAMRUZZAMAN
    SPECIAL THANKS: JANE ALAM JAN (MODIFIED DESCRIPTION, DATASET)
    题意:让你补一些字母,使所给的串成为回文串,要求补的这些字母的个数最少。
    思路:区间dp;
    dp[i][j]表示,i,j这段成为回文的最少要添加的字母的个数。
    状态转移方程:如果str[i]==str[j];那么dp[i][j]=dp[i+1][j-1];也就是在区间i+1,j-1;已经成为回文,那么两端再添加两个相同的字符依然是回文,
    当str[i]!=str[j]的时候,那么在区间i+1,j-1;已经成为回文,那么考虑是在是添加一个字母和str[i]相同,还是和str[j]相同,dp[i][j]=min(dp[i+1][j],dp[i][j-])+1;
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<string.h>
     6 #include<queue>
     7 #include<stack>
     8 #include<math.h>
     9 using namespace std;
    10 int dp[200][200];
    11 char str[200];
    12 char astr[200];
    13 int main(void)
    14 {
    15     int i,j,k;
    16     int s;
    17     scanf("%d",&k);
    18     for(s=1; s<=k; s++)
    19     {
    20         scanf("%s",str);
    21         int l=strlen(str);
    22         int u=0;
    23         for(i=l-1; i>=0; i--)
    24         {
    25             astr[u++]=str[i];
    26         }
    27         for(i=0; i<200; i++)
    28         {
    29             for(j=0; j<200; j++)
    30             {
    31                 dp[i][j]=1e9;
    32             }
    33         }
    34         int maxx=0;
    35         for(i=0; i<l; i++)
    36         {
    37             for(j=i; j>=0; j--)
    38             {
    39                 if(i==j)
    40                 {
    41                     dp[j][i]=0;
    42                 }
    43                 else
    44                 {
    45                     if(str[i]==str[j])
    46                     {
    47                         dp[j][i]=dp[j+1][i-1];
    48                         if(j+1>i-1)
    49                         {
    50                             dp[j][i]=0;
    51                         }
    52                     }
    53                     else
    54                     {
    55                         dp[j][i]=min(dp[j][i-1],dp[j+1][i])+1;
    56                     }
    57                 }
    58             }
    59         }
    60         printf("Case %d: %d
    ",s,dp[0][l-1]);
    61     }
    62     return 0;
    63 }
    油!油!you@
  • 相关阅读:
    从源码角度看MySQL memcached plugin——3. containers表的管理
    从源码角度看MySQL memcached plugin——2. 线程模型和连接的状态机
    从源码角度看MySQL memcached plugin——1. 系统结构和引擎初始化
    从源码角度看MySQL memcached plugin——0.大纲
    关注博客
    文摘
    MySQL—FTS实现原理介绍PPT
    GIT情况展示说明
    final发布48小时用户调查报告
    final版本发布评价II
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5610809.html
Copyright © 2020-2023  润新知