zoukankan      html  css  js  c++  java
  • POJ 1159 Palindrome

    实质是:求一个字符串与其逆字符串的LCS.

    LCS用short int开数组刚好可以卡着内存过!!! 

                                                          Palindrome
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 46566   Accepted: 15910

    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

    5Ab3bd

    Sample Output

    2

    Source

     
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 
     7 short int dp[5555][5555];
     8 
     9 int main()
    10 {
    11     char s1[5555],s2[5555];
    12     s1[0]=s2[0]=':';
    13     int n;
    14     cin>>n;
    15     for(int i=1;i<=n;i++)
    16       {
    17           cin>>s1[i];
    18           s2[i]=s1[i];
    19       }
    20     reverse(s2+1,s2+1+n);
    21 //    cout<<s2;
    22     memset(dp,0,sizeof(dp));
    23 
    24     for(int i=1;i<=n;i++)
    25         for(int j=1;j<=n;j++)
    26     {
    27         if(s1[i]==s2[j])
    28             dp[i][j]=dp[i-1][j-1]+1;
    29         else
    30             dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
    31     }
    32 
    33     cout<<n-dp[n][n]<<endl;
    34 
    35     return 0;
    36 }
    37  

     当然,也可以用滚存:

    11613675 CKboss 1159 Accepted 792K 1110MS G++ 635B 2013-05-19 15:51:30
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 
     7 int dp[2][5555];
     8 
     9 int main()
    10 {
    11     char s1[5555],s2[5555];
    12     s1[0]=s2[0]=':';
    13     int n;
    14     cin>>n;
    15     for(int i=1;i<=n;i++)
    16       {
    17           cin>>s1[i];
    18           s2[i]=s1[i];
    19       }
    20     reverse(s2+1,s2+1+n);
    21 //    cout<<s2;
    22     memset(dp,0,sizeof(dp));
    23 
    24     for(int i=1;i<=n;i++)
    25         for(int j=1;j<=n;j++)
    26     {
    27         if(s1[i]==s2[j])
    28             dp[i%2][j]=dp[(i-1)%2][j-1]+1;
    29         else
    30             dp[i%2][j]=max(dp[(i-1)%2][j],dp[i%2][j-1]);
    31     }
    32 
    33     cout<<n-dp[n%2][n]<<endl;
    34 
    35     return 0;
    36 }
  • 相关阅读:
    数据结构与算法(一):初识算法和计算模型
    Flutter学习笔记(41)--自定义Dialog实现版本更新弹窗
    Mybatis-Plus简介及HelloWorld实现(附视频教程)~连载中
    “乐观锁”解决高并发下的幂等性问题(附java实测视频教程)
    git merge&rebase区别
    GIT TAG标签使用
    idea操作github远程库分支
    idea操作GIT本地库分支操作
    eclipse GIT本地库分支操作
    git分支操作命令
  • 原文地址:https://www.cnblogs.com/CKboss/p/3087550.html
Copyright © 2011-2022 走看看