hdu 2610 Sequence one(深度搜索)

简介:

Sequence one

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 294    Accepted Submission(s): 97


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.
 

 

Input
The input contains multiple test cases.
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.
分析:
(1)题意很简单就是在给定的序列中找到固定个数的递增的子序列,如果子序列的总个数少于要求的个数,那么就把所有的子序列输出即可,注意每组测试用例就为有一空行。
(2)技巧一:重判,这里有两个重判,第一个重判是判断如果搜索的是子序列的第一个元素,那么判断从原始序列开始到当前位置是否已经出现过该元素,若出现过则之前肯定搜索过该元素,则放弃该元素的搜索。第二个重判,当搜索的不是子序列的第一个元素时,则判断子序列的前一个元素对应原始序列的位置,然后从该位置下一个元素开始到到当前搜索的位置之前判断该元素是否出现过,如果出现过,说明该子串出现过重复的,则放弃该元素。这里的两个重判需要好好地想想,很巧妙。
(3)技巧二:剪枝,这里的一个剪枝技巧是做了一个标记位,假如我在搜索长度为3的子串时,发现没有一个符合的,那么就不可能存在长度为4的子串符合条件。如果没有这个剪枝就会超时,看来影响很大的。。。。。
复制代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

//len:搜索的长度,count:记录有多少个串了
int n,p,len,count_num;
int num[1001];

//做一个标记,如果一个短的串都不能够找到,
//那么就长的串就更不可能找到了,这里的一个巧妙地剪枝如果没用就会超时
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;
}
复制代码

 









本文转自NewPanderKing51CTO博客,原文链接:http://www.cnblogs.com/newpanderking/archive/2012/10/11/2719941.html ,如需转载请自行联系原作者



相关文章
|
C语言
【C语言】循环结构程序设计(第二部分 -- 习题讲解)
【C语言】循环结构程序设计(第二部分 -- 习题讲解)
159 1
|
网络安全 开发工具 git
Unable to negotiate with xx.xxx.xxxx port 22: no matching host key type found. Their offer: ssh-rsa(解决的两种方式)
Unable to negotiate with xx.xxx.xxxx port 22: no matching host key type found. Their offer: ssh-rsa(解决的两种方式)
5851 0
Unable to negotiate with xx.xxx.xxxx port 22: no matching host key type found. Their offer: ssh-rsa(解决的两种方式)
|
11月前
|
机器学习/深度学习 数据采集 人工智能
深入探索人工智能与大数据的融合之路
本文旨在探讨人工智能(AI)与大数据技术如何相互促进,共同推动现代科技的进步。通过分析两者结合的必要性、挑战以及未来趋势,为读者提供一个全面的视角,理解这一领域内的最新发展动态及其对行业的影响。文章不仅回顾了历史背景,还展望了未来可能带来的变革,并提出了几点建议以促进更高效的技术整合。
|
11月前
|
人工智能 自然语言处理 搜索推荐
智能语音助手的发展与未来:开启人机交互的新篇章
智能语音助手的发展与未来:开启人机交互的新篇章
1746 28
|
人工智能 弹性计算 定位技术
【云故事探索】NO.4: 千寻位置,时空智能赋能行业数字化转型
千寻位置,成立于2015年,利用北斗卫星系统及全球5000多座增强站,提供厘米级定位服务。该公司借助阿里云的计算能力,为汽车、农业等多个行业提供高精度时空智能解决方案,推动行业转型升级。千寻已完成超130亿元估值的A轮融资,展现了其在时空智能领域的领先地位。通过云上部署,千寻优化服务质量和市场扩展,应对突发流量,计划进一步全球化并应用AI技术。阿里云的支持对于千寻的成功至关重要,双方合作将时空智能服务推向国际。
【云故事探索】NO.4: 千寻位置,时空智能赋能行业数字化转型
|
存储 Swift iOS开发
使用Swift开发一个简单的iOS应用的详细步骤。
使用Swift开发iOS应用的步骤包括:创建Xcode项目,设计界面(Storyboard或代码),定义数据模型,实现业务逻辑,连接界面和逻辑,处理数据存储(如Core Data),添加网络请求(必要时),调试与测试,根据测试结果优化改进,最后提交至App Store或其它平台发布。
324 0
|
存储 SQL 关系型数据库
(六)MySQL索引原理篇:深入数据库底层揭开索引机制的神秘面纱!
《索引原理篇》它现在终于来了!但对于索引原理及底层实现,相信大家多多少少都有了解过,毕竟这也是面试过程中出现次数较为频繁的一个技术点。在本文中就来一窥`MySQL`索引底层的神秘面纱!
856 7
|
安全 NoSQL Redis
服务器又被攻击了,我这样做...
近期遭遇阿里云服务器频繁报警,经分析发现是由于测试服务器所有端口对公网开放,导致自动化程序对其扫描。黑客可能利用类似Redis的未授权访问漏洞进行攻击。为避免此类问题,建议:1. 不开放不必要的端口;2. 避免以root权限运行服务;3. 设置服务器IP白名单;4. 定期更换密码。保持良好安全习惯可保障服务器安全。
3958 4
服务器又被攻击了,我这样做...
|
前端开发 JavaScript
canvas系列教程01——直线、三角形、多边形、矩形、调色板
canvas系列教程01——直线、三角形、多边形、矩形、调色板
411 0
|
算法 UED
探索编程思维:不仅是代码,更是解决问题的艺术
【5月更文挑战第24天】 在数字世界的舞台上,编程不单是一系列指令的排列组合,它更是一种独特的思维方式。本文将深入探讨编程思维的本质及其在问题解决过程中的应用。我们将剖析编程思维如何影响逻辑构建、创新思考和系统分析,并通过实例说明如何将编程原则应用于日常生活和非技术领域。