zoukankan      html  css  js  c++  java
  • HDU 1513 Palindrome【LCS】

    题意:给出一个字符串s,问至少加入多少个字母让它变成回文串

    解题思路:求出该字符串与该字符串翻转后的最长公共子序列的长度,再用该字符串的长度减去最长公共子序列的长度即为所求

    反思:因为题目所给的n的范围为3<=n<=5000,所以dp[][]数组如果开到dp[5005][5005],会超内存,此时应该就用滚动数组来优化

    滚动数组的详细介绍http://blog.csdn.net/niushuai666/article/details/6677982

    Palindrome

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3301    Accepted Submission(s): 1140

    Problem 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
     
    #include<stdio.h>
    #include<string.h>
    char s[5005],w[5005];
    int dp[2][5005];
    int max(int a,int b)
    {
    	if(a>b)
    	return a;
    	else
    	return b;
    }
    int main()
    {
    	int n,i,j,x,y;
    	while(scanf("%d",&n)==1)
    	{
    		scanf("%s",&s);
    		for(i=0;i<n;i++)
    		w[i]=s[n-1-i];
    		w[i]='';
    		memset(dp,0,sizeof(dp));
    		
    		for(i=1;i<=n;i++)
    		{
    			
    			for(j=1;j<=n;j++)
    			{
    			   x=i%2;
    			   y=1-x;
    			  if(s[i-1]==w[j-1])
    			  dp[x][j]=dp[y][j-1]+1;
    			  else
    			  dp[x][j]=max(dp[y][j],dp[x][j-1]);
    		    }
    		}
    		printf("%d
    ",n-dp[n%2][n]);
    	}
    }
    

      

     
  • 相关阅读:
    函数的随机梯度下降(固定函数与自定义函数随机梯度下降)
    常见六种随机变量分布可视化
    基于beautifulSoup进行电影网站排名的获取与格式化输出
    基于numpy实现矩阵计算器
    中文论文-LaTex模板
    论文阅读:Deformable ConvNets v2
    darknet loss可视化软件
    《为什么精英都是时间控》读书总结
    Window下Latex加速编译方法以及西农毕设论文模板推荐
    时间加数字问题
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4176173.html
Copyright © 2011-2022 走看看