Sequence one
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 893 Accepted Submission(s): 355
Problem 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 5 1 3 2 3 6 1 3 2 4 100 1 2 3 2
Sample Output
1 3 2 1 3 1 2 1 3 2 1 3 1 2 1 2 3 1 2 1 3 2 3 2 2 1 2 3 1 2 2
Hint
Hint : You must make sure each subsequence in the subsequences is unique.
这一题思路很简单,就是不断增大子序列的长度,搜索满足条件的情况;
重点在判重:第一个重判是判断如果搜索的是子序列的第一个元素,那么判断从原始序列开始到当前位置是否已经出现过该元素,若出现过则之前
肯定搜索过该元素,则放弃该元素的搜索。第二个重判,当搜索的不是子序列的第一个元素时,则判断子序列的前一个元素对应原始序列的位置,
然后从该位置下一个元素开始到到当前搜索的位置之前判断该元素是否出现过,如果出现过,说明该子串出现过重复的,则放弃该元素。
重点在剪枝:一个标记位,假如我在搜索长度为3的子串时,发现没有一个符合的,那么就不可能存在长度为4的子串符合条件。
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#define N 1005
using namespace std;
int n, p;
int a[N]; //用于存储输入的n个数据
int index[N]; //index[i]表示:当前加入到目前子序列中的元素在a[]数组中的下标,这样做便于输出结果;
int cnt; //用于存储当前所形成的的子序列的个数, 这样可以看看到没到p个;
int len; //用于表示当前正在形成的子序列所要达到的长度,长度达到要求就可以输出了;
bool flag; //所有子序列的情况都列出还没到p,则直接跳出;若还有没列的情况,flag会更新为true;
bool check(int index1, int index2){ //判断从index1到index2是否有重复的元素
for(int i = index1; i < index2; i++){
if(a[i] == a[index2]) return false;
}
return true;
}
void dfs(int l, int pos){ //l:当前正在形成的子序列的现有长度, pos当前搜索到a[]序列的那个位置;
int i;
if(cnt >= p) //当前已经够数了,不必再搜了
return;
if(l == len){ //如果当前长度正好达到要求长度,则输出;
flag = true; //还可以继续搜索,情况还没有搜索完;
cnt++; //当前是一种情况;
for(i = 0; i < len-1; i++){
printf("%d ", a[index[i]]);
}
printf("%d\n", a[index[i]]);
return; //这个return非常的重要,你当前长度都达到了len就没必要在进入for循环了
}
for(i = pos; i < n; i++){
if(!l || (l&&a[index[l-1]] <= a[i])){ //能进行搜索的情况
if(!l && !check(0, i)){ //对于不能成为子序列的第一个元素,判重1
continue;
}
else if(l && !check(index[l-1]+1, i)){ //对于不能要成为子序列的其他元素,判重2;
continue;
}
index[l] = i;
dfs(l+1, i+1);
}
}
return;
}
int main(){
while(scanf("%d%d", &n, &p)!= EOF){
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
}
cnt = 0;
for(int i=1; i <= n; i++){ //当前搜索的子串的长度
len = i;
flag = false; //所有子序列的情况都列出还没到p,则直接跳出;若还有没列的情况,flag会更新为true;
dfs(0, 0);
if(cnt >= p || !flag) break; //达到p个或者没办法找到p个都跳出循环
}
printf("\n");
}
return 0;
}