1 条题解

  • 1
    @ 2026-5-23 19:35:02
    #include<bits/stdc++.h> 
    using namespace std;
    #define LL long long
    const int N=1e3+11;
    struct node
    {
    	int data,i,j;
    }dp[N][N];
    int lena,lenb;
    string a,b,ans;
    int main()
    {
    	cin>>a>>b;
    	lena=a.size();
    	lenb=b.size();
    	for(int i=1;i<=lena;i++)
    	{
    		for(int j=1;j<=lenb;j++)
    		{
    			if(a[i-1]==b[j-1])
    			{
    				dp[i][j].data=dp[i-1][j-1].data+1;
    				dp[i][j].i=i;
    				dp[i][j].j=j;
    			}
    			else if(dp[i-1][j].data>dp[i][j-1].data)
    			{
    				dp[i][j].data=dp[i-1][j].data;
    				dp[i][j].i=dp[i-1][j].i;
    				dp[i][j].j=dp[i-1][j].j;
    			}
    			else
    			{
    				dp[i][j].data=dp[i][j-1].data;
    				dp[i][j].i=dp[i][j-1].i;
    				dp[i][j].j=dp[i][j-1].j;
    			} 
    		}
    	}
    	cout<<dp[lena][lenb].data<<endl;
    	for(int i=dp[lena][lenb].i,j=dp[lena][lenb].j;i|j;)
    	{
    		ans=a[i-1]+ans;
    		int tmp=dp[i-1][j-1].i;
    		j=dp[i-1][j-1].j;
    		i=tmp;
    	}cout<<ans;
       	return 0;
    }

    信息

    ID
    1508
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    281
    已通过
    51
    上传者