简单例题:
https://vijos.org/p/1111
AC代码:
#include<iostream>//c++ #include<cmath>//数学公式 #include<cstdlib>//malloc #include<cstring> #include<string> #include<cstdio>//输入输出 #include<algorithm>//快排 #include<queue>//队列 #include<functional>//优先队列 #include<stack>//栈 #include<vector>//容器 #include<map>//地图 if continue typedef long long ll; const int N=105; const int inf=0x7fffffff; using namespace std; char a[N],b[N]; int dp[N][N]; int main() { //freopen("C:\\Users\\ch\\Desktop\\1.txt","r",stdin); //freopen("C:\\Users\\lenovo\\Desktop\\2.txt","w",stdout); int i,j,k; while(~scanf("%s %s",a,b)) { memset(dp,0,sizeof(dp)); for(i=0;i<strlen(a);i++) for(j=0;j<strlen(b);j++) if(a[i]==b[j]) dp[i+1][j+1]=dp[i][j]+1; else dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]); cout<<strlen(a)+strlen(b)-dp[strlen(a)][strlen(b)]<<endl; } return 0; }
改进(空间)
AC代码:
#include<cstdio> #include<cstring> #include<string> #include<iostream> using namespace std; int dp[105],i,j,ans,tmp; char a[105],b[105]; int main() { while(~scanf("%s%s",a,b)) { memset(dp,0,sizeof(dp)); for(i=0;i<strlen(a);i++) { ans=0; //若a[i]==b[j], dp[i][j] = dp[i-1][j-1]+1 //否则,dp[i][j] = max(dp[i-1][j], dp[i][j-1]) //此处进行了空间优化,ans 代表 dp[i-1][j-1] //dp[j-1] 代表 dp[i][j-1], dp[j] 代表 dp[i-1][j] for(j=0;j<strlen(b);j++) { tmp=dp[j]; if(a[i]==b[j]) dp[j]=ans+1; else if(dp[j]<dp[j-1]) dp[j]=dp[j-1]; ans=tmp; } } printf("%d\n",strlen(a)+strlen(b)-dp[j-1]); } }
中等:
http://acm.hdu.edu.cn/showproblem.php?pid=1513
AC 代码:
#include<cstdio> #include<cstring> #include<string> #include<iostream> using namespace std; char s1[5005],s2[5005]; int dp[5005]; int lcs(int n) { memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++) for(int j=0,ans=0;j<n;j++) { int tmp=dp[j]; if(s1[i]==s2[j]) dp[j]=ans+1; else dp[j]=max(dp[j],dp[j-1]); ans=tmp; } } int main() { int i,j,n; while(~scanf("%d",&n)) { scanf("%s",s1);//for(i = 0;i<n;i++) s2[i] = s1[n-1-i];s2[i] = '\0'; strcpy(s2,s1); strrev(s2); lcs(n); printf("%d\n",n-dp[n-1]); } return 0; }
http://acm.hdu.edu.cn/showproblem.php?pid=1758
AC代码:
#include<stdio.h> #include<string.h> #include<iostream> using namespace std; const int N=10001; char s1[N][40],s2[N][40]; int dp[N],ans,tmp; int main() { while(1) { int i=-1,j=-1; while(cin>>s1[++i],s1[i][0]!='#') {} while(cin>>s2[++j],s2[j][0]!='#') {} memset(dp, 0, sizeof(dp)); int lenS1=i,lenS2=j; for(i=0; i<lenS1; i++) { ans=0; for(j=0;j<lenS2;j++) { tmp = dp[j]; if(strcmp(s1[i],s2[j])) dp[j] = ans+1; else if(dp[j-1]>dp[j]) dp[j]=dp[j-1]; ans = tmp; } } printf("%d\n", dp[lenS2-1]); } return 0; }
https://www.bnuoj.com/v3/problem_show.php?pid=14378
#include <iostream> #include <cstring> #include <cstdio> #define LMT 2003 #define eps 1e8 using namespace std; int dp[LMT][LMT]; char a[LMT],b[LMT]; int max(const int &a,const int &b) { return a>b?a:b; } int main(void) { int la,lb,ans=0; a[0]=b[0]=1; scanf("%s%s",&a[1],&b[1]); la=strlen(a);lb=strlen(b); la--;lb--; for(int i=1;i<=la;i++) for(int j=1;j<=lb;j++) ans=max(ans,dp[i][j]=dp[i-1][j-1]+(a[i]==b[j])); printf("%d\n",lb-ans); return 0; }