给定两个字符串s1s2...sn和t1t2...tn。求出这两个字符串最长的公共子序列的长度。
1<=n,m<=1000
输入:
abcd
becd
输出:
3(bcd)
#include<bits/stdc++.h> using namespace std; char s[1005]; char t[1005]; int dp[1005][1005]; int main(){ gets(s); gets(t); int n = strlen(s); int m = strlen(t); for(int i = 0; i < n; i ++){ for(int j = 0; j < n; j ++){ if(s[i] == t[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<<dp[n][m]; return 0; }
标签:int,char,公共,序列,1005,strlen,最长,dp From: https://www.cnblogs.com/8023yyl/p/17073847.html