Pku acm 1458 Common Subsequence 动态规划解析
2012-08-17 22:50
459 查看
详细解析请见~~ http://blog.csdn.net/yangshuolll/article/details/7879349
先写出这个状态转移方程:
E[i][j]=min{ D(i-1,j)+xi ,D(i,j-1)+yi,D(i-1,j-1)+Zi,j }
brute force 方法 超时:有重项 递归调用结果:
进行剪枝:
#include<stdio.h>
#include<string.h>
char a[1000],b[1000];
int len1,len2,max;
int c[1000][1000];
void cmp(int i,int j,int s)
{
if(i==len1||j==len2){if(max<s)max=s;return;}
if(a[i]==b[j]){if(s>=c[i][j]){c[i][j]=s ;cmp(i+1,j+1,s+1);}}
else{
if(s>=c[i][j]){
c[i][j]=s;
cmp(i,j+1,s);
cmp(i+1,j,s);
}
}
}
int main()
{
while(scanf("%s",a)!=EOF)
{
max=0;
int i,j;
for(i=0;i<1000;i++)
for(j=0;j<1000;j++)
c[i][j]=0;
scanf("%s",b);
len1=strlen(a);
len2=strlen(b);
cmp(0,0,0);
printf("%d\n",max);
}
}
上面还是会超时:
下面用 动态规划进行处理 详细解析请见~~ http://blog.csdn.net/yangshuolll/article/details/7879349
贴代码 正确代码:
post code
#include<stdio.h>
#include<string.h>
char a[1000],b[1000];
int c[1000][1000];
int max(int a,int b)
{
if(a>b)return a;
return b;
}
int main()
{
int i,j,len1,len2;
while(scanf("%s",a)!=EOF)
{
scanf("%s",b);
len1=strlen(a);
len2=strlen(b);
for(i=0;i<1000;i++)
c[i][0]=c[0][i]=0;
for(i=1;i<=len1;i++)
for(j=1;j<=len2;j++)
{
if(a[i-1]==b[j-1])c[i][j]=c[i-1][j-1]+1;
else {
c[i][j]=max(c[i-1][j],c[i][j-1]);
}
}
printf("%d\n",c[len1][len2]);
}
}
先写出这个状态转移方程:
E[i][j]=min{ D(i-1,j)+xi ,D(i,j-1)+yi,D(i-1,j-1)+Zi,j }
brute force 方法 超时:有重项 递归调用结果:
#include<stdio.h> #include<string.h> char a[10000],b[10000]; int len1,len2,max; int sum=0; void cmp(int i,int j,int s) { if(i==len1||j==len2){if(max<s)max=s;return;} if(a[i]==b[j])cmp(i+1,j+1,s+1); else{ cmp(i,j+1,s); cmp(i+1,j,s); } } int main() { while(scanf("%s",a)) { sum=0; max=0; scanf("%s",b); len1=strlen(a); len2=strlen(b); cmp(0,0,0); printf("%d\n",max); } }
进行剪枝:
#include<stdio.h>
#include<string.h>
char a[1000],b[1000];
int len1,len2,max;
int c[1000][1000];
void cmp(int i,int j,int s)
{
if(i==len1||j==len2){if(max<s)max=s;return;}
if(a[i]==b[j]){if(s>=c[i][j]){c[i][j]=s ;cmp(i+1,j+1,s+1);}}
else{
if(s>=c[i][j]){
c[i][j]=s;
cmp(i,j+1,s);
cmp(i+1,j,s);
}
}
}
int main()
{
while(scanf("%s",a)!=EOF)
{
max=0;
int i,j;
for(i=0;i<1000;i++)
for(j=0;j<1000;j++)
c[i][j]=0;
scanf("%s",b);
len1=strlen(a);
len2=strlen(b);
cmp(0,0,0);
printf("%d\n",max);
}
}
上面还是会超时:
下面用 动态规划进行处理 详细解析请见~~ http://blog.csdn.net/yangshuolll/article/details/7879349
贴代码 正确代码:
post code
#include<stdio.h>
#include<string.h>
char a[1000],b[1000];
int c[1000][1000];
int max(int a,int b)
{
if(a>b)return a;
return b;
}
int main()
{
int i,j,len1,len2;
while(scanf("%s",a)!=EOF)
{
scanf("%s",b);
len1=strlen(a);
len2=strlen(b);
for(i=0;i<1000;i++)
c[i][0]=c[0][i]=0;
for(i=1;i<=len1;i++)
for(j=1;j<=len2;j++)
{
if(a[i-1]==b[j-1])c[i][j]=c[i-1][j-1]+1;
else {
c[i][j]=max(c[i-1][j],c[i][j-1]);
}
}
printf("%d\n",c[len1][len2]);
}
}
相关文章推荐
- Pku acm 1458 Common Subsequence 题意分析
- PKU 1458 Common Subsequence(最长公共子序列,dp,简单)
- poj 1458 小白算法练习 Common Subsequence 动态规划
- 字符串 并且 动态规划练习二:HDU ACM 1159 Common Subsequence
- POJ 1458-Common Subsequence (动态规划)
- Common Subsequence poj-1458 动态规划
- pku1458-----Common Subsequence(经典动态规划题)
- poj 动态规划DP - 1458 Common Subsequence
- POJ 1458 Common Subsequence (DP 动态规划)
- PKU-1458 Common Subsequence (最长公共子序列LCS)
- 动态规划 POJ - 1458 Common Subsequence(最长公共子序列)
- PKUOJ1458 Common Subsequence
- POJ 1458 Common Subsequence(最长公共子序列)__动态规划
- POJ 1458 Common Subsequence 【最长公共子序列】
- POJ 题目1458 Common Subsequence(最长公共子序列长度纯模板)
- POJ 1458 Common Subsequence——LCS
- POJ 1458 - Common Subsequence - 最长公共子序列-详解
- Common Subsequence (poj 1458) 最长公共子序列
- POJ 1458-Common Subsequence(线性dp/LCS)
- LintCode Longest Common Subsequence(最长公共子序列长度,动态规划入门题)