zoukankan      html  css  js  c++  java
  • OpenJudge/Poj 1159 Palindrome

    1.链接地址:

    http://bailian.openjudge.cn/practice/1159/

    http://poj.org/problem?id=1159

    2.题目:

    Palindrome
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 49849   Accepted: 17153

    Description

    A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome.

    As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.

    Input

    Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from 'A' to 'Z', lowercase letters from 'a' to 'z' and digits from '0' to '9'. Uppercase and lowercase letters are to be considered distinct.

    Output

    Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.

    Sample Input

    5
    Ab3bd

    Sample Output

    2

    Source

    3.思路:

    这题要知道其实最少增加的个数= 字符串总字数 - LCS(最长公共子序列)

    所以就转化为求LCS

    LCS为典型的dp算法之一,时间复杂度O(n^2),空间复杂度O(n)

    !!!这题用string会超时,郁闷。开始对string没好感

    4.代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 
     5 #define max(a,b) ((a) > (b) ? (a) : (b))
     6 
     7 using namespace std;
     8 
     9 
    10 int same(char ch1,char ch2)
    11 {
    12     if(ch1 == ch2) return 1;
    13     else return 0;
    14 }
    15 
    16 int LCS(char *str1,char *str2,int len1,int len2)
    17 {
    18     int i,j;
    19 
    20     //if(len1 < len2) {char *str3 = str1;str1 = str2;str2 = str3;}
    21 
    22     int **dp = new int*[2];
    23     for(i = 0; i < 2; ++i) dp[i] = new int[len2 + 1];
    24     memset(dp[0],0,sizeof(int) * (len2 + 1));
    25     dp[1][0] = 0;
    26 
    27     
    28     for(i = 1; i <= len1; ++i)
    29     {
    30         for(j = 1; j <= len2; ++j)
    31         {
    32             dp[i % 2][j] = max(dp[(i - 1) % 2][j],max(dp[i % 2][j - 1],dp[(i - 1) % 2][j - 1] + same(str1[i - 1],str2[j - 1])));
    33             //cout<<"dp[" << i << "][" << j << "]=" << dp[i % 2][j] << endl;
    34         }
    35     }
    36     int max = dp[len1 % 2][len2];
    37 
    38     for(i = 0; i < 2; ++i) delete [] dp[i];
    39     delete [] dp;
    40 
    41     return max;
    42 }
    43 
    44 int main()
    45 {
    46     int n;
    47     cin>>n;
    48 
    49     char *str1 = new char[n];
    50     char *str2 = new char[n];
    51 
    52     int i;
    53     for(i = 0; i < n; ++i)
    54     {
    55         cin>>str1[i];
    56         str2[n - 1 - i] = str1[i];
    57     }
    58 
    59     int lcs_len = LCS(str1,str2,n,n);
    60 
    61     cout<<(n - lcs_len)<<endl;
    62 
    63     delete [] str1;
    64     delete [] str2;
    65     return 0;
    66 }
  • 相关阅读:
    windows 安装 nodejs指定版本
    Xshell通过ssh登录远程服务器(秘钥)
    CentOS7 防火墙操作
    Linux系统MySQL开启远程连接
    PHP 下载图片文件并压缩文件成zip
    thinkphp 中更新数据字段,同时某字段值++操作(报错TP5.1不支持的数据表达式:[exp]的解决办法)
    layui的loading加载中
    Linux下面安装swoole
    windows 下cmd命令删除文件或者文件夹
    PHP 删除某目录下的全部文件
  • 原文地址:https://www.cnblogs.com/mobileliker/p/3551985.html
Copyright © 2011-2022 走看看