UVA 497 Strategic Defense Initiative【最长严格递增子序列长度及打印】
来源:互联网 发布:北大青鸟陈璇java视频 编辑:程序博客网 时间:2024/05/09 14:08
题目大意:同标题。
解题策略:输入要注意,降序输出LIS可以用一个简单的递归搞定,升序花了点功夫。
/* UVA 497 Strategic Defense Initiative AC by J.Dark ON 2013/4/1 Time 0.012s*/ #include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;const int maxn = 10000;int num[maxn], length[maxn], mark[maxn], numCount;void Solve(){ for(int i=1; i<=numCount; i++) length[i] = 1; for(int i=1; i<=numCount; i++) mark[i] = -1; //找出最大长度及序列 for(int i=1; i<=numCount; i++){ for(int j=1; j<i; j++){ if(num[j] < num[i]){ //找出num[i]可跟在哪些数字之后 if(length[j]+1 > length[i]){ length[i] = length[j]+1; mark[i] = j; //num[i]接在num[j]之后 } } } } int maxAns = 0, maxPos; //找出上升序列最大长度 最大上升序列最末元素位置 for(int i=1; i<=numCount; i++){ if(maxAns < length[i]){ maxAns = length[i]; maxPos = i; } } int LIS[maxn], k = maxPos; for(int i=maxAns; i>0; i--){ //记录LIS LIS[i] = num[k]; k = mark[k]; } printf("Max hits: %d\n", maxAns); for(int i=1; i<=maxAns; i++) cout << LIS[i] << endl;} ///////////////////////////////////int main(){ int testCase; char temp[50]; while(cin >> testCase) { getchar(); //万恶的输入,浪费时间…… getchar(); for(int i=0; i<testCase; i++){ numCount = 0; while(gets(temp) && strlen(temp)) { num[++numCount] = atoi(temp); //表示atoi函数让我感觉之前动不动字符流的方法土爆了…… } if(i > 0) cout << endl; Solve(); } } //system("pause"); return 0;}
- UVA 497 Strategic Defense Initiative【最长严格递增子序列长度及打印】
- Uva 497 - Strategic Defense Initiative
- UVa 497 - Strategic Defense Initiative
- uva 497 Strategic Defense Initiative
- 求一个序列中的最长严格递增子序列。
- 最长递增子序列长度+最长公共子序列
- 最长递增子序列长度+最长公共子序列
- 求最长递增子序列的长度
- 最长递增子序列长度问题
- 求最长连续递增子序列长度
- 最长递增子序列的长度
- 最长递增子序列长度算法
- 最长递增子序列的长度
- 【算法】最长递增子序列的长度
- 最长单调递增子序列的长度
- 求解最长递增子序列的长度
- 求最长递增子序列的长度
- 最长公共子序列 LCS 【长度及打印】
- Windows下sqlplus “/as sysdba”登陆报“ORA-01031: insufficient privileges”处理
- Linux多线程编程(不限Linux)
- 每月自评之三:2013年3月
- Linux 的多线程编程的高效开发经验
- 《Objective-C编程之道:iOS设计模式解析》学习笔记——第1章
- UVA 497 Strategic Defense Initiative【最长严格递增子序列长度及打印】
- eclipse 引入java包
- vi学习笔记
- powermanagerservice分析
- 代码大全第二版(第一章):欢迎进入软件构建的世界
- 第三次实验
- 常用Hash算法(C语言实现)
- 闭散列表及其查找算法的实现
- c int 转string 和string 转int