标签:lcis
题意:求最长公共上升子序列的长度代码:
#include <algorithm> #include <iostream> #include <sstream> #include <cstdlib> #include <cstring> #include <iomanip> #include <cstdio> #include <string> #include <bitset> #include <vector> #include <queue> #include <stack> #include <cmath> #include <list> #include <map> #include <set> #define sss(a,b,c) scanf("%d%d%d",&a,&b,&c) #define mem1(a) memset(a,-1,sizeof(a)) #define mem(a) memset(a,0,sizeof(a)) #define ss(a,b) scanf("%d%d",&a,&b) #define s(a) scanf("%d",&a) #define p(a) printf("%d\n", a) #define INF 0x3f3f3f3f #define w(a) while(a) #define PI acos(-1.0) #define LL long long #define eps 10E-9 #define N 100000 #define mod 100000000 using namespace std; void mys(int& res) { int flag=0; char ch; while(!(((ch=getchar())>='0'&&ch<='9')||ch=='-')) if(ch==EOF) res=INF; if(ch=='-') flag=1; else if(ch>='0'&&ch<='9') res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0'; res=flag?-res:res; } void myp(int a) { if(a>9) myp(a/10); putchar(a%10+'0'); } /*************************THE END OF TEMPLATE************************/ int dp[550][550]; int a[550]; int b[550]; int main(){ int t, m, n; s(t); w(t--){ mem(dp); dp[1][1] = dp[2][1] = dp[1][2] = 1; s(m); for(int i=1; i<=m; i++) s(a[i]); s(n); for(int i=1; i<=n; i++) s(b[i]); for(int i=1; i<=m; i++){ int bg = 0; for(int j=1; j<=n; j++){ if(bg < dp[i-1][j] && a[i] > b[j]) bg = dp[i-1][j];//a[i] > b[j] 是为了保证当a[i] == b[j] 能有dp[i][j] = bg + 1; dp[i][j]= dp[i-1][j]; if(a[i] == b[j]) dp[i][j] = bg + 1; } } int ans = 0; for(int i=1; i<=n ;i++){ if(dp[m][i] > ans) ans = dp[m][i]; } p(ans); if(t) printf("\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:lcis
原文地址:http://blog.csdn.net/bigsungod/article/details/47253529