Codeforces 10D LCIS seeking longest common subsequence and outputs the rising sequences dp

Disclaimer: This article is a blogger original article, shall not be reproduced without the consent of bloggers.

https://blog.csdn.net/qq574857122/article/details/34430283

Topic links: Click on the link to open

Meaning of the questions:

Given a sequence of length n

To give a sequence of fixed length k

LCIS ​​and outputs the required sequence

If multiple solutions Solution output random. .

= - = knock when listening to the pre meaning little song not want to know. Never occurred before. .


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
#include<vector>
#include<map>
#include<math.h>
#include<string>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 505
int a[N],s[N];
int n, k;
int dp[N][N];
int pre[N][N][2];
int is[N][N];
vector<int>G;
int main(){
	int i,j,co;
	while(~scanf("%d",&n)){
		G.clear();
		for(i=1;i<=n;i++)scanf("%d",&a[i]);
		scanf("%d",&k);
		for(i=1;i<=k;i++)scanf("%d",&s[i]);
		memset(dp, 0, sizeof dp);
		memset(pre, 0, sizeof pre);
		memset(is, 0, sizeof is);
		for(i=1;i<=n;i++) {
			int maxx = 0;
			int x = 0, y = 0;
			for(j=1;j<=k;j++)
			{
				dp[i][j] = dp[i-1][j];
				if(a[i]>s[j] && maxx < dp[i-1][j]) {
					maxx = dp[i-1][j];
					if(is[i-1][j])
						x = i-1, y = j;
					else {
						x = pre[i-1][j][0];
						y = pre[i-1][j][1];
					}
				}
				if(a[i]==s[j]) {
					is[i][j] = 1;
					dp[i][j] = maxx + 1;
					if(is[x][y])
						pre[i][j][0] = x, pre[i][j][1] = y;
					else {
						pre[i][j][0] = pre[x][y][0];
						pre[i][j][1] = pre[x][y][1];
					}
					continue;
				}
				if(is[i-1][j])
					pre[i][j][0] = i-1, pre[i][j][1] = j;
				else {
					pre[i][j][0] = pre[i-1][j][0];
					pre[i][j][1] = pre[i-1][j][1];
				}
			}
		}
		int ans = 0;
		int x = n, y = k;
		for(i=1;i<=k;i++)if(ans<dp[n][i]){
			ans = dp[n][i];
			x = n, y = i;
		}
		printf("%d\n",ans);
		if(!ans)continue;
		while(x+y) {
			if(is[x][y])G.push_back(a[x]);
			int x1 = pre[x][y][0];
			int y1 = pre[x][y][1]; 
			x = x1 , y = y1;
		}
		for(i=G.size()-1; i>=0; i--){
			printf("%d",G[i]);
			i?

printf(" "):puts(""); } } return 0; } /* 2 1 2 3 1 2 3 */



Guess you like

Origin www.cnblogs.com/ldxsuanfa/p/10963921.html