zoukankan      html  css  js  c++  java
  • 51nod 1092 回文字符串(dp)

    基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
    收藏
    关注
    取消关注
    回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。每个字符串都可以通过向中间添加一些字符,使之变为回文字符串。
    例如:abbc 添加2个字符可以变为 acbbca,也可以添加3个变为 abbcbba。方案1只需要添加2个字符,是所有方案中添加字符数量最少的。
    Input
    输入一个字符串Str,Str的长度 <= 1000。
    Output
    输出最少添加多少个字符可以使之变为回文字串。
    Input示例
    abbc
    Output示例
    2

    求逆串与原串的最长公共子序列(可不连续),结果就为字符串长度-最长公共子序列长度。

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 using namespace std;
     6 int dp[1005][1005];
     7 char str[1005];
     8 char tmp[1005];
     9 int main()
    10 {
    11     ios::sync_with_stdio(false);
    12     while(~scanf("%s",str+1))
    13     {
    14         strcpy(tmp+1,str+1);
    15         int len=strlen(tmp+1);
    16         reverse(tmp+1,tmp+len+1);
    17         memset(dp,0,sizeof(dp));
    18         for(int i=1;i<=len;i++)
    19             for(int j=1;j<=len;j++)
    20         {
    21             if(str[i]==tmp[j])
    22                 dp[i][j]=max(dp[i-1][j-1]+1,dp[i][j]);
    23             else
    24                 dp[i][j]=max(max(dp[i][j],dp[i-1][j]),dp[i][j-1]);
    25         }
    26         cout<<len-dp[len][len]<<endl;
    27     }
    28     return 0;
    29 }
    View Code
     
  • 相关阅读:
    前台js加密实例
    Redis 核心原理
    Rredis的安装与常用命令
    Zookeeper的源码环境的搭建和源码解读
    Zookeeper集群搭建
    Zookeeper的客户端API使用
    Zookeeper介绍
    HashMap的死锁 与 ConcurrentHashMap
    定时任务 & 定时线程池 ScheduledThreadPoolExecutor
    Fork/Join框架
  • 原文地址:https://www.cnblogs.com/onlyli/p/7261515.html
Copyright © 2011-2022 走看看