hdu 2610+hdu 2611(两道很好的搜索题)

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2610

http://acm.hdu.edu.cn/showproblem.php?pid=2611

很好的两道搜索题,都用到了判重。。。orz....不怎么会,看了大牛的解题报告才理解。。。跪神牛

大牛的hdu 2610思路:题意很简单就是在给定的序列中找到固定个数的递增的子序列,如果子序列的总个数少于要求的个数,那么就把所有的子序列输出即可,注意每组测试用例就为有一空行。

技巧一:重判,这里有两个重判,第一个重判是判断如果搜索的是子序列的第一个元素,那么判断从原始序列开始到当前位置是否已经出现过该元素,若出现过则之前肯定搜索过该元素,则放弃该元素的搜索。第二个重判,当搜索的不是子序列的第一个元素时,则判断子序列的前一个元素对应原始序列的位置,然后从该位置下一个元素开始到到当前搜索的位置之前判断该元素是否出现过,如果出现过,说明该子串出现过重复的,则放弃该元素。这里的两个重判需要好好地想想,很巧妙。
技巧二:剪枝,这里的一个剪枝技巧是做了一个标记位,假如我在搜索长度为3的子串时,发现没有一个符合的,那么就不可能存在长度为4的子串符合条件。如果没有这个剪枝就会超时,看来影响很大的
View Code
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 #define MAXN 1010
 6 struct Node{
 7     int num,pos;
 8 }path[MAXN];
 9 
10 int num[MAXN];
11 int n,p,_count,len;
12 bool flag;
13 
14 bool Judge(int st,int ed){
15     for(int i=st;i<ed;i++){
16         if(num[i]==num[ed])return false;
17     }
18     return true;
19 }
20 
21 void dfs(int l,int pos){
22     if(_count>=p)return ;
23     if(l==len){
24         _count++;
25         flag=true;
26         for(int i=0;i<l-1;i++){
27             printf("%d ",path[i].num);
28         }
29         printf("%d\n",path[l-1].num);
30         return ;
31     }
32     for(int i=pos;i<n;i++){
33         if((l!=0&&path[l-1].num<=num[i])||l==0){
34             if(l!=0&&!Judge(path[l-1].pos+1,i))continue;
35             if(l==0&&!Judge(0,i))continue;
36             path[l].num=num[i];
37             path[l].pos=i;
38             dfs(l+1,i+1);
39         }
40     }
41 }
42 
43 
44 int main(){
45     while(~scanf("%d%d",&n,&p)){
46         for(int i=0;i<n;i++)
47             scanf("%d",&num[i]);
48         _count=0;
49         for(int i=1;i<n;i++){
50             flag=false;
51             len=i;
52             dfs(0,0);
53             if(_count>=p||(!flag))break;
54         }
55         puts("");
56     }
57     return 0;
58 }

hdu 2611题目描述:前一题sequence one 属于一类题,都是dfs,首先排一次序,然后再检查时注意输入时的下标,生成的子串不能够出现下标非递增的,也就是首先子串时递增的,其次下标也是递增的,然后不能有重复的子串出现。

技巧:判重的技巧,这里我们可以一开始设置一个flag=false,第一次的时候该flag的为true,然后用一个pre保留当前位置的数,然后后面在搜相同len的序列时,如果当前的数与pre是一样的,说明先前已经搜过了,直接continue就行了,否则,pre就保留这个数,然后搜len+1的数。。。这里的flag和pre用的是太妙了。。。orz...ym!!!

View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 #define MAXN 110
 7 struct Node{
 8     int num,pos;
 9 }node[MAXN];
10 int n,p,len,l,_count;
11 int path[MAXN];
12 
13 int cmp(const Node &p,const Node &q){
14     if(p.num!=q.num)
15         return p.num<q.num;
16     return p.pos<q.pos;
17 }
18 
19 bool dfs(int l,int pos,int repos){ 
20     if(l==len){
21         _count++;
22         for(int i=0;i<l-1;i++){
23             printf("%d ",path[i]);
24         }
25         printf("%d\n",path[l-1]);
26         if(_count==p)return true;
27         return false;
28     }
29     int pre;
30     bool flag=false;//flag和pre的判重妙用,得仔细体会;
31     for(int i=pos;i<=n;i++){
32         //由于生成的子串是不能出现下标非递增的。
33         if(node[i].pos>repos){
34             if(!flag){flag=true;pre=node[i].num;}//判重
35             else if(pre==node[i].num)continue;//判重
36             pre=node[i].num;//不相等的话保留当前的数,然后进入下一个dfs继续搜;
37             path[l]=node[i].num;
38             if(dfs(l+1,i+1,node[i].pos))return true;
39         }
40     }
41     return false;
42 }
43 
44 
45 
46 int main(){
47     while(~scanf("%d%d",&n,&p)){
48         for(int i=1;i<=n;i++){
49             scanf("%d",&node[i].num);
50             node[i].pos=i;
51         }
52         sort(node+1,node+n+1,cmp);
53         _count=0;
54         for(int i=1;i<n;i++){
55             len=i;
56             if(dfs(0,1,0))break;
57         }
58         puts("");
59     }
60     return 0;
61 }
原文地址:https://www.cnblogs.com/wally/p/3074356.html