hdu 2610

来源:互联网 发布:如何求矩阵的伴随矩阵 编辑:程序博客网 时间:2024/06/05 05:01
/* * 2610_1.cpp * *  Created on: 2013年8月17日 *      Author: Administrator */#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>using namespace std;//len:搜索的长度,count:记录有多少个串了int n, p, len, count_num;int num[1001];//num[] :用来记录序列中的每一个数字//做一个标记,如果一个短的串都不能够找到,//那么就长的串就更不可能找到了,这里的一个巧妙地剪枝如果没用就会超时bool flag;typedef struct {int n, pos;} Tem;Tem tem[1001];//用来保存非递减序列的各个元素及其所在的位置//若在产生序列的前一个数字到当前这个数字中,//出现等于num[e]的,那么说明之前已经有序列选择了num[e],bool check(int s, int e) {for (int i = s + 1; i < e; i++)if (num[i] == num[e])return false;return true;}void print_sequence(int length) {for (int i = 0; i < length - 1; i++)cout << tem[i].n << " ";cout << tem[length - 1].n << endl;}//dep:搜索的深度,也就是目前搜索到子串的长度//pos: 当前搜索的位置void dfs(int dep, int pos) {//搜索到的串的数量已经>=指定串的数量if (count_num >= p)return;//搜索到了if (dep == len) {//当前搜索的深度==指定搜索的深度count_num++;flag = true;print_sequence(len);//已经搜索到符合的字串了return;}for (int i = pos; i < n; i++) {if ((dep != 0 && tem[dep - 1].n <= num[i]) || dep == 0) {if (dep == 0 && !check(-1, i))continue;if (dep != 0 && !check(tem[dep - 1].pos, i))continue;tem[dep].n = num[i];tem[dep].pos = i;dfs(dep + 1, i + 1);}}return;}int main() {while (cin >> n >> p) {for (int i = 0; i < n; i++)cin >> num[i];count_num = 0;for (int i = 1; i < n; i++) {flag = false;len = i;dfs(0, 0);if (count_num >= p || !flag)break;}cout << endl;}return 0;}

原创粉丝点击