标签:style color 使用 strong 数据 io
1、先说说杭电的1159吧!
这道题是基础动规,比较简单!
就是要你求最长的公共子序列(不要优化)
动态转移方程: dp[i+1][j+1]=(a[i]=b[i])?dp[i][j]+1:max(dp[i+1][j],dp[i][j+1])
AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 520
char a[N],b[N];
int dp[N][N];
int main()
{
int i,j,len_a,len_b;
while(scanf("%s %s",a,b)!=EOF)
{
len_a=strlen(a);
len_b=strlen(b);
for(i=1;i<=len_a;i++)
for(j=1;j<=len_b;j++)
{
if(a[i-1]==b[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
printf("%d\n",dp[len_a][len_b]);
}
return 0;
}题意是给你一个字符串,要你算出它成为回文串需要多少字母
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 5005
int dp[2][N];
char str[N],cstr[N];
int main()
{
int i,j,t;
scanf("%d",&t);
scanf("%s",str);
for(i=0;i<t;i++){
cstr[i]=str[t-i-1];
dp[0][i]=dp[1][i]=0;
}
dp[0][t]=dp[1][t]=0;
for(i=1;i<=t;i++)
for(j=1;j<=t;j++)
{
if(str[i-1]==cstr[j-1])
dp[i%2][j]=dp[(i-1)%2][j-1]+1;
else
dp[i%2][j]=max(dp[(i-1)%2][j],dp[i%2][j-1]);
}
printf("%d\n",t-dp[t%2][t]);
return 0;
}
POJ 1159 Palindrome && HDU 1159 Common Subsequence,布布扣,bubuko.com
POJ 1159 Palindrome && HDU 1159 Common Subsequence
标签:style color 使用 strong 数据 io
原文地址:http://blog.csdn.net/u012313382/article/details/38024293