码迷,mamicode.com
首页 > 其他好文 > 详细

51Nod 1092 回文字符串 | 最长公共子序列变形

时间:2017-09-05 16:48:34      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:images   子序列   ges   def   clu   char   log   bsp   span   

 

技术分享

 

求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长

#include "stdio.h"
#include "string.h"
#define maxn 1005
char s[maxn],s1[maxn];
int dp[maxn][maxn];
int main()
{
    int n=0,i,j,len;
    scanf("%s",s);
    len=strlen(s);
    strcpy(s1,s);
    strrev(s1);
    for(i=0;i<len;i++)
    {
        for(j=0;j<len;j++)
        {
            if(s[i]==s1[j])
             dp[i+1][j+1]=dp[i][j]+1;
            else
             dp[i+1][j+1]=dp[i+1][j]>dp[i][j+1]?dp[i+1][j]:dp[i][j+1];
        }
    }
    printf("%d\n",len-dp[len][len]);
}

 

51Nod 1092 回文字符串 | 最长公共子序列变形

标签:images   子序列   ges   def   clu   char   log   bsp   span   

原文地址:http://www.cnblogs.com/kimsimple/p/7479097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!