hdu2611 Sequence two 深搜
来源:互联网 发布:数据库日志分析工具 编辑:程序博客网 时间:2024/05/22 10:35
此题和2610差不多。只是要求输出的子序列要从小到大排列。如1 3 2的子序列要写成 {1},{2},{3},{1,2},{1,3}。这就要求在输入数据的时候用结构体保存数据和当前数据的位置。
在进行深度搜索的时候要先对数据进行排序,如果数据相同。就将位置小到大排序。对于1 3 2 ,排序后是1 2 3,子序列{2,3}是不能出现的,因为2的位置比3大 原数列是不会构成这样的子序列的。
这两题的总结搜索方式:记录深度deep和当前搜索位置position。
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<vector>#include<cmath>using namespace std;//len:搜索的长度,tmp:记录有多少个串了int a[1001];int n,p,len,tmp;struct node { int num; int pos;};node tree[1001];bool cmp(const node& x,const node& y){ if(x.num!=y.num) return x.num<y.num; else return x.pos<y.pos;}/*bool find(int x,int y){ for(int i=x+1;i<y;i++) if(a[i] == a[y]) return false; return true;}*/void print(int x){ int i; for(i=0;i<x-1;i++) printf("%d ",a[i]); printf("%d\n",a[i]);}bool dfs(int deep,int pos,int pp){ int pre,i; bool flag=false; if(deep == len) { tmp++; print(deep); if(tmp == p) return true; return false; } if(pos>n) return false; for(i=pos;i<n;i++) { if(tree[i].pos > pp ) { if(!flag) { pre=tree[i].num; flag=true; } else if(pre == tree[i].num) continue; pre=tree[i].num; a[deep]=tree[i].num; if(dfs(deep+1,i+1,tree[i].pos)) return true; } } return false;}int main(){ while(~scanf("%d%d",&n,&p)) { int tc; for(int i=0;i<n;i++) { scanf("%d",&tc); tree[i].num=tc; tree[i].pos=i; } sort(tree,tree+n,cmp); tmp=0; for(int i=1;i<n;i++) { len=i; if(dfs(0,0,-1)) break; } printf("\n"); } return 0;}
- hdu2611 Sequence two 深搜
- 【DFS+小操作判重】【HDU2610+HDU2611】Sequence
- Sequence two(hdu2611dfs)
- HDU 2611 Sequence two
- acm 2 1025 Sequence two
- hdu 2611 Sequence two 4.3.6
- HDU Sequence two 2611 DFS搜索判重
- hdu 5288 OO’s Sequence(two point + rmq)
- hdu 5288 OO’s Sequence(two pointers)
- HDU 5806 NanoApe Loves Sequence Ⅱ two pointers
- HDU5806 NanoApe Loves Sequence Ⅱ BestCoder Round #86 two-pointer
- HDU5806 NanoApe Loves Sequence Ⅱ(尺取法\two pointers)
- HDU 2611 Sequence two (dfs + 重判 + 剪枝)
- Sequence one(深搜)
- sequence
- SEQUENCE
- Sequence
- sequence
- HDU Bone Collector 01背包模版应用
- MFC:CImage显示OpenCV:Mat矩阵图像
- hdu 1542 Atlantis 线段树+矩形面积并+离散化点
- java以UTF-8编码写入文本文件
- HDU-2010
- hdu2611 Sequence two 深搜
- 【rzxt】系统设置Excel单元格数据的对齐方式技巧
- 详解Oracle多种表连接方式
- (ubuntu 9.04) sudo apt-get update 错误处理
- 转载_Android开发者必知的开发资源
- 小孩花钱租 月入七八千
- nyoj 232 How to eat more Banana
- 芯嵌stm32入门教程目录
- 《士兵突击》