1 solutions

  • 0
    @ 2022-5-11 22:36:47
    #include<algorithm>
    using namespace std;
    const int N=1010;
    int n;
    int a[N],b[N];
    int f[N][N];
    //f[i][j]表示的是从所有在a中前i个元素中出现,在b中前j个元素中出现的子序列 
    int main(){
        cin>>n;
    	for(int i=1;i<=n;i++) cin>>a[i];
    	for(int j=1;j<=n;j++) cin>>b[j];
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=n;j++){
    			f[i][j]=max(f[i-1][j],f[i][j-1]);
    			if(a[i]==b[j]) f[i][j]=f[i-1][j-1]+1;
    		}
    	}
    	cout<<f[n][n]<<endl; 
    	
    	return 0;
    }`
    
    • 1

    Information

    ID
    1157
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    8
    Tags
    # Submissions
    19
    Accepted
    5
    Uploaded By