您的位置:首页 > 其它

hdu 1513(dp+滚动数组)

2013-09-11 10:32 148 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513

思路:n这么大,可以采用滚动数组,然后就是求原串和反串的LCS了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int dp[2][5555];
char str1[5555],str2[5555];
int n;

int main()
{
while(~scanf("%d",&n)){
scanf("%s",str1);
strcpy(str2,str1);
reverse(str2,str2+n);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(str1[i-1]==str2[j-1]){
dp[i%2][j]=dp[(i-1)%2][j-1]+1;
}else
dp[i%2][j]=max(dp[i%2][j-1],dp[(i-1)%2][j]);
}
}
printf("%d\n",n-dp[n%2]
);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: