NC213136. SubsequencePair
描述
输入描述
The input consists of several test cases terminated by end-of-file. For each test case:
The first line contains a string s. The second line contains a string t.
*
*
* The sum of |s| does not exceed 20000.
* The sum of |t| does not exceed 20000.
* Both the strings consist only of English lowercase letters.
输出描述
For each test case, output the sum of |x| and |y|.
示例1
输入:
aaaa bbbb abcd abca abcd abcd
输出:
8 7 8
C++(clang++11) 解法, 执行用时: 55ms, 内存消耗: 15992K, 提交时间: 2020-11-02 14:31:54
#include<bits/stdc++.h> using namespace std; int dp[2005][2005]; char s[2005],t[2005]; int main(){ while(~scanf("%s%s",s+1,t+1)){ int ns=strlen(s+1),nt=strlen(t+1),ans=0; for(int i=1;i<=ns;++i) for(int j=1;j<=nt;++j){ dp[i][j]=max(dp[i-1][j],dp[i][j-1]); if(s[i]==t[j]) dp[i][j]=dp[i-1][j-1]+2,ans=max(ans,dp[i][j]+nt-j); else if(s[i]<t[j])ans=max(ans,dp[i-1][j-1]+2+ns+nt-i-j); } ans=max(ans,dp[ns][nt]); printf("%d\n",max(ans,nt)); } }