ACM--steps--4.3.7--Sequence one
来源:互联网 发布:阿里云还是腾讯云 编辑:程序博客网 时间:2024/06/05 06:13
Sequence one
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 271 Accepted Submission(s): 77Problem Description
Search is important in the acm algorithm. When you want to solve a problem by using the search method, try to cut is very important.
Now give you a number sequence, include n (<=1000) integers, each integer not bigger than 2^31, you want to find the first P subsequences that is not decrease (if total subsequence W is smaller than P, than just give the first W subsequences). The order of subsequences is that: first order the length of the subsequence. Second order the sequence of each integer’s position in the initial sequence. For example initial sequence 1 3 2 the total legal subsequences is 5. According to order is {1}; {3}; {2}; {1,3}; {1,2}. {1,3} is first than {1,2} because the sequence of each integer’s position in the initial sequence are {1,2} and {1,3}. {1,2} is smaller than {1,3}. If you also can not understand , please see the sample carefully.
Now give you a number sequence, include n (<=1000) integers, each integer not bigger than 2^31, you want to find the first P subsequences that is not decrease (if total subsequence W is smaller than P, than just give the first W subsequences). The order of subsequences is that: first order the length of the subsequence. Second order the sequence of each integer’s position in the initial sequence. For example initial sequence 1 3 2 the total legal subsequences is 5. According to order is {1}; {3}; {2}; {1,3}; {1,2}. {1,3} is first than {1,2} because the sequence of each integer’s position in the initial sequence are {1,2} and {1,3}. {1,2} is smaller than {1,3}. If you also can not understand , please see the sample carefully.
Input
The input contains multiple test cases.
Each test case include, first two integers n, P. (1<n<=1000, 1<p<=10000).
Each test case include, first two integers n, P. (1<n<=1000, 1<p<=10000).
Output
For each test case output the sequences according to the problem description. And at the end of each case follow a empty line.
Sample Input
3 51 3 23 61 3 24 1001 2 3 2
Sample Output
1321 31 21321 31 21231 21 32 32 21 2 31 2 2HintHint : You must make sure each subsequence in the subsequences is unique.#include<iostream>#include<cstring>using namespace std;int dyx[1009];int n,p;//n表示母列中所包含的数字的个数,而p则表示需要求的是几个固定个数的递增子列,如果不够则将全部输出.int len,cont;//len表示进行搜索的长度,cont表示已经搜索了几个串了.struct name{ int now,pos;//now记录在子列中的数,pos记录对应的数字在母列当中的位置.};name wyx[10009];bool flag;//做出一个标记,假如在搜索长度为3的子串时没有符合标准的,那么长度为4的子串自然也不存在.bool check(int st,int ed){ for(int i=st+1;i<ed;i++) { if(dyx[i]==dyx[ed]) return false; } return true;}void set(int length){ for(int i=0;i<length-1;i++) cout<<wyx[i].now<<" "; cout<<wyx[length-1].now<<endl;//最后一个后面没有空格。}void dfs(int dep,int pos){ //dep表示当前搜索的深度,当前搜索的是第几个 //pos表示在当前搜索的子列中的第几个位置. if(cont>=p)//子列个数到了 return; if(dep==len)//搜索到了 { cont++;//有符合标准的字串长度加1; flag=true; set(len); return; } //首先这里面也有两个剪枝 //第一个剪枝,如果当前搜索的是子列的第一个位置,那么从母列中的第一个元素到当前位置的元素是否有相等的情况。 //如果出现了相等的情况的话,则舍弃,因为之前肯定搜索过该元素。 //第二个剪枝,如果搜索的不是当前子列的第一个位置的话,那么从当前搜索的子列的前一个元素在母列当中的位置开始 //一直搜索到当前位置之前的一个元素,看是否出现过相同的元素,如果出现过当前的元素,则放弃该元素的搜索. for(int i=pos;i<n;i++) { //判断是否递增; //可以出现相等的情况 if((dep!=0&&wyx[dep-1].now<=dyx[i])||dep==0) { //第一个剪枝的情况。 if(dep==0&&!check(-1,i)) continue; if(dep!=0&&!check(wyx[dep-1].pos,i)) continue; wyx[dep].now=dyx[i]; wyx[dep].pos=i; dfs(dep+1,i+1); } } return;}int main(){ while(cin>>n>>p) { for(int i=0;i<n;i++) cin>>dyx[i]; cont=0; for(int i=1;i<n;i++) { flag=false; len=i; dfs(0,0); if(cont>=p||!flag) break; } cout<<endl; } return 0;}
Author
yifenfei
Source
奋斗的年代
Recommend
yifenfei
0 0
- ACM--steps--4.3.7--Sequence one
- ACM Steps之一(Section One)
- ACM Steps之二 (Section One)
- [HDU]ACM Steps-Chapter One-Section 1
- [HDU]ACM Steps-Chapter One-Section 2
- [HDU]ACM Steps-Chapter One-Section 3
- acm 2 1024 Sequence one
- hdu ACM Steps 做题笔记(Chapter One)
- ACM--steps--4.3.5--Sticks
- hdu acm steps 1.3.7
- hdu acm steps 2.1.7
- hdu acm steps 2.2.7
- hdu ACM STEPS 1.2.7
- Steps-ACM
- ACM STEPS
- ACM steps
- ACM--steps--4.3.2--Prime Ring Problem
- ACM--steps--4.3.3--蜘蛛牌
- div布局(并列div的位置)
- tomcat运行framework7
- POJ 3714 Raid(最近点对)
- 第一篇文章
- Linux下core文件产生的一些注意问题
- ACM--steps--4.3.7--Sequence one
- POSIX的pthread_join
- Android 实用工具Hierarchy Viewer实战
- 在oracle存储过程中创建临时表
- java 初识
- TCP协议中的三次握手和四次挥手(图解)
- R语言学习——2.数据输入
- js模板引擎介绍搜集
- 容器