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 }
  • 相关阅读:
    SAP C4C OBN(Object Based Navigation)不能工作的原因分析
    使用SAP C4C自定义BO association创建动态下拉列表
    如何使用SAP HANA Studio的PlanViz分析CDS view性能问题
    如何使用jMeter测试SAP OData服务并发访问时的性能
    OData服务在SAP CRM,Cloud for Customer和S/4HANA上的实现比较
    SAP UI5和Vue的数据双向绑定实现原理比较
    在SAP WebClient UI里显示倒数计时的UI
    【年度重磅】2020华为云社区年度技术精选合集,700页+免费下载!
    面试必问:如何实现Redis分布式锁
    聊聊架构模式的变迁:从分层架构到微服务架构
  • 原文地址:https://www.cnblogs.com/CKboss/p/3087550.html
Copyright © 2011-2022 走看看