See LCS again
来源:互联网 发布:淘宝情侣店铺有真的吗 编辑:程序博客网 时间:2024/04/23 18:37
See LCS again
时间限制:1000 ms | 内存限制:65535 KB
难度:3
- 描述
There are A, B two sequences, the number of elements in the sequence is n、m;
Each element in the sequence are different and less than 100000.
Calculate the length of the longest common subsequence of A and B.
- 输入
- The input has multicases.Each test case consists of three lines;
The first line consist two integers n, m (1 < = n, m < = 100000);
The second line with n integers, expressed sequence A;
The third line with m integers, expressed sequence B; - 输出
- For each set of test cases, output the length of the longest common subsequence of A and B, in a single line.
- 样例输入
5 41 2 6 5 41 3 5 4
- 样例输出
3
//无语了,什么都不想说。。。#include <stdio.h>#include <string.h>int mark[100001], data[100001];int binarySearch(int key, int n) //二分查找大于等于且距离key最近的数的位置{ int left = 0, right = n, mid; while(left <= right) { mid = (left + right) / 2; if(mark[mid] >= key) { right = mid - 1; } else { left = mid + 1; } } return left;}/*void fun() //测试二分查找是否正确{ int i, n, temp; scanf("%d", &n); for(i = 0; i < n; i++) { scanf("%d", &data[i]); } while(scanf("%d", &temp) != EOF) { printf("%d\n", binarySearch(temp, n-1)); }}*/int main(){ // fun(); int n, m, i, temp, top, ans; while(~scanf("%d%d", &n, &m)) { memset(mark, 0, sizeof(mark)); top = 0; for(i = 1; i <= n; i++) { scanf("%d", &temp); mark[temp] = i; //将原序列单调上升编号,并标记每个数 } for(i = 1; i <= m; i++) { scanf("%d", &temp); if(mark[temp]) { data[top++] = mark[temp]; //将两个序列中都有的数,以在第二个序列中的相对位置存入新序列 } } ans = 0; mark[ans++] = data[0]; for(i = 1; i < top; i++) //判断单调最长子序列 { if(data[i] > mark[ans-1]) { mark[ans++] = data[i]; } else { mark[binarySearch(data[i], ans-1)] = data[i]; } } printf("%d\n", ans); } return 0;}
0 0
- See LCS again
- ayit See LCS again
- See LCS again
- See LCS again
- See LCS again
- See LCS again
- NYOJ 760 See LCS again
- NYOJ 760 See LCS again
- Nyoj 760 See LCS again [Lcs]
- nyistOJ-See LCS again(二分)
- NYOJ 760 - See LCS again(LCS转LIS)
- NYOJ 题目760 See LCS again(LIS求LCS)
- nyoj See LCS again 760 (LCS&&LIS) 好题
- nyoj 760 See LCS again 最长公共子序列
- NYOJ760-See LCS again,有技巧的暴力!
- See LCS again 最长递增子序列到最长公共子序列的转化
- Song -----See You Again
- See You Again
- Lua字符串及字符串匹配
- 为什么项目要使用DIRECTORY_SEPARATOR做分割符
- html学习笔记三
- DOM方法
- 使用Screen后台执行任务,防止SSH中断
- See LCS again
- 杭电1106
- 给定字符串String str=“aaaaabbbbbccccddddaa”,输出 a7b5c4d4;
- 网易ios 面试
- Linux禁止Ctrl+Alt+Del重启
- Eclipse无法导入项目(提示can not import* because the project name is in user)
- 链表笔试面试题
- 第十二周项目四银行系统任务二---引入函数改善程序结构
- EncodeConvert编码转换工具