[ACM_动态规划] UVA 12511 Virus [最长公共递增子序列 LCIS 动态规划]
2014-04-07 22:13
337 查看
Virus |
However, it is infected by a virus, so random records are inserted (but the order of original events is preserved). The backup log file is also infected, but since the virus is making changes randomly, the two logs are now different.
Given the two infected logs, your task is to find the longest possible original log file. Note that there might be duplicated timestamps in an infected log, but the original log file will not have duplicated timestamps.
Input
The first line contains T ( T![](http://uva.onlinejudge.org/external/125/12511img1.png)
100), the number of test cases. Each of the following lines contains two lines, describing the two logs in the same format. Each log starts with an integer n ( 1
![](http://uva.onlinejudge.org/external/125/12511img1.png)
n
![](http://uva.onlinejudge.org/external/125/12511img1.png)
1000), the number of events in the log, which is followed by n positive integers not greater than 100,000, the timestamps of the events, in the same order as they appear in the log.
Output
For each test case, print the number of events in the longest possible original log file.Sample Input
1 9 1 4 2 6 3 8 5 9 1 6 2 7 6 3 5 1
Sample Output
3 题目大意:T种情况,每种情况2行数据,每行数据第一个表示个数,接下来是一个序列,问两组数据的最长公共递增子序列的长度。 解题思路:当看到这题想到的是LCS和LIS问题,没错这题也是动态规划问题,只要找到状态转移方程就可轻易搞定! >_<:LIS设DP[i]表示以第i个数字结尾的最长上升子序列的长度 >0<:DP[i]=max(DP[j]+1){1<=j<=i-1} >_<:LCS设DP[i][j]表示以A串第i个字符结尾以B串第j个字符结尾的最长字串 >0<:当a[i]==b[j]时:DP[i][j]=DP{i-1][j-1]+1; 当a[i]!=b[j]时:DP[i][j]=max(DP[i-1][j],DP[i][j-1]) >_<:LCIS设F[i][j]表示以a串前i个字符b串的前j个字符且以b[j]为结尾构成的LCIS的长度 >0<:当a[i]!=b[j]时:F[i][j]=F[i-1][j] 当a[i]==b[j]时:F[i][j]=max(F[i-1][k])+1 1<=k<=j-1 && b[j]>b[k]
#include<iostream> #include<cstdio> #include<string> #include<string.h> #include<cstring> #include<cmath> #include<sstream> #include<iomanip> #include<algorithm> #include<vector> using namespace std; int f[1005][1005],a[1005],b[1005],i,j,t,n1,n2,maxn; int main() { scanf("%d",&t); while(t--) { scanf("%d",&n1); for(i=1;i<=n1;i++) scanf("%d",&a[i]); scanf("%d",&n2); for(i=1;i<=n2;i++) scanf("%d",&b[i]); memset(f,0,sizeof(f)); for(i=1;i<=n1;i++) { maxn=0; for(j=1;j<=n2;j++) { f[i][j]=f[i-1][j];//不相等 if (a[i]>b[j]&&maxn<f[i-1][j]) maxn=f[i-1][j];//更新maxn if (a[i]==b[j]) f[i][j]=maxn+1;//相等 } } maxn=0; for(i=1;i<=n2;i++)if(maxn<f[n1][i])maxn=f[n1][i]; printf("%d\n",maxn); } return 0; }
相关文章推荐
- J - Virus UVA - 12511——最长上升递增子序列
- 动态规划 - 最长公共子序列 - 最长公共子串 - 最长不重复子串 - 最长递增子序列 - 最长回文子串
- 动态规划 最长公共子序列 LCS,最长单独递增子序列,最长公共子串
- J - Virus UVA - 12511——最长上升递增子序列
- 动态规划——最大子序列、最长递增子序列、最长公共子串、最长公共子序列、字符串最小编辑距离日记整理
- 【动态规划】最长递增子序列代码(UVA 10131)
- ACM模板——最长公共上升子序列 LCIS
- HDU 4512 吉哥系列故事——完美队形I LCIS 最长递增公共子序列
- 最长公共递增子序列(LCIS)
- 动态规划求一道 类最长递增公共子序列
- 动态规划求一道 类最长递增公共子序列
- UVA12511 - Virus(DP+最长公共上升子序列)
- 最大子序列、最长递增子序列、最长公共子串、最长公共子序列、字符串编辑距离
- Codeforces Beta Round #10-D. LCIS(最长上升公共子序列)
- Is Bigger Smarter?+uva+简单dp(最长公共升降子序列的变形)
- 数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- 最大子序列、最长递增子序列、最长公共子串、最长公共子序列、字符串编辑距离
- 求解最大子序列、最长递增子序列、最长公共子串、最长公共子序列
- 找工作知识储备(2)---数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- UVa 111 - History Grading 最长递增子序列 LIS