hdu 2610 2611 dfs的判重技巧
对于全排列枚举的数列的判重技巧
1:如果查找的是第一个元素 那么 从0开始到当前的位置看有没有出现过这个元素 出现过就pass
2: 如果查找的不是第一个元素 那么 从查找的子序列当前位置的前一个元素对应原序列的位置一直到查到到元素的位置看是否出现过。,。 出现过就pass
2610
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std; //len:搜索的长度,count:记录有多少个串了
int n,p,len,count_num;
int num[]; //做一个标记,如果一个短的串都不能够找到,
//那么就长的串就更不可能找到了,这里的一个巧妙地剪枝如果没用就会超时
bool flag; typedef struct
{
int n,pos;
}Tem; Tem tem[]; //若在产生序列的前一个数字到当前这个数字中,
//出现等于num[e]的,那么说明之前已经有序列选择了num[e],
bool check(int s,int e)
{
for(int i = s+; i < e; i++)
if(num[i]==num[e])return false;
return true;
} void print_sequence(int length)
{
for(int i =; i < length-;i++)
cout<<tem[i].n<<" ";
cout<<tem[length-].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!=&&tem[dep-].n<=num[i])||dep==)
{
if(dep==&&!check(-,i))//当是第一个元素的时候
continue;
if(dep!=&&!check(tem[dep-].pos,i))//不是第一个元素的时候
continue;
tem[dep].n = num[i];
tem[dep].pos = i;
dfs(dep+,i+);
}
}
return;
} int main()
{
while(cin>>n>>p)
{
for(int i=;i<n;i++)
cin>>num[i];
count_num =;
for(int i =;i < n;i++)
{
flag=false;
len = i;
dfs(,);
if(count_num>=p||!flag)break;
}
cout<<endl;
}
return;
}
2611
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 101
using namespace std;
struct node
{
int data,pos;
}fuck[maxn];
int n,m,cnt,len,liner[maxn];
int cmp(node a,node b)
{
if(a.data==b.data) return a.pos<b.pos;
else return a.data<b.data;
}
void dfs(int dep,int pos,int prelpos)
{
if(cnt>m) return;
if(dep==len)
{
cnt++;
if(cnt>m) return;
for(int i=;i<dep;i++)
{
if(i==) cout<<liner[i];
else cout<<' '<<liner[i];
}
cout<<endl;
return ;
}
int pre,f=;//对一层内的数的判重 pre 用来记录前一次出现的数(每次都要更新,, 这里错了一次)
for(int i=pos;i<=n;i++)
{
if(fuck[i].pos>prelpos)
{
if(f==)//先初始化f为0 第一次的时候用来记录
{
f=-;
pre=fuck[i].data;
}
else if(pre==fuck[i].data) continue;//应为是排好序的 所以 同层里面 用过的就直接 continue
pre=fuck[i].data;
liner[dep]=fuck[i].data;
dfs(dep+,i+,fuck[i].pos);
if(cnt>m) return;
}
}
}
int main()
{
while(cin>>n>>m)
{
for(int i=;i<=n;i++)
{
cin>>fuck[i].data;
fuck[i].pos=i;
}
cnt=;
sort(fuck+,fuck++n,cmp);
for(int i=;i<n;i++)
{
len=i;
dfs(,,);
if(cnt>m) break; }
cout<<endl;
}
return;
}
hdu 2610 2611 dfs的判重技巧的更多相关文章
- POJ 3050 枚举+dfs+set判重
思路: 枚举+搜一下+判个重 ==AC //By SiriusRen #include <set> #include <cstdio> using namespace std; ...
- 【BZOJ】1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(暴力dfs+set判重)
http://www.lydsy.com/JudgeOnline/problem.php?id=1675 一开始我写了个枚举7个点....... 但是貌似... 写挫了. 然后我就写dfs.. 判重好 ...
- hdu 4821 字符串hash+map判重 String (长春市赛区I题)
http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这 ...
- HDU 5679 Substring 后缀数组判重
题意:求母串中有多少不同的包含x字符的子串 分析:(首先奉上FZU官方题解) 上面那个题就是SPOJ694 ,其实这两个题一样,原理每次从小到大扫后缀sa数组,加上新的当前后缀的若干前缀,再减去重复的 ...
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
Sum It Up Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- 【DFS+小操作判重】【HDU2610+HDU2611】Sequence
题意 2610 按照长度优先 位置次之 输出所有不递减序列 2611 按照长度优先 大小次之 输出所有不递减序列 题解不写了 来源于http://www.cnblogs.com/wally/archi ...
- UVa 10400 - Game Show Math 游戏中的数学 dfs+判重
题意:给出一些数字和一个目标数字,要求你在数字间添加+-*/,让表达式能达到目标数字,运算符号的优先级都是一样的. 由于数据量很大,本来想用map<string>判重的,结果还是超时了,然 ...
- hdu 4444 Walk (离散化+建图+bfs+三维判重 好题)
Walk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
- HDU2579--Dating with girls(2)--(DFS, 判重)
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
随机推荐
- python定时任务实现
安装 pip install schedule 示例代码 import schedule import time def job(): print("I'm working..." ...
- Mybatis按照SQL查询字段的顺序返回查询结果,使用resultType="java.util.LinkedHashMap"
在使用Mybatis开发时,Mybatis返回的结果集就是个map,当返回map时只需要做好SQL映射就好了,减少了代码量,简单便捷,缺点是不太方便维护,但是写大量的vo类去返回也挺累的,这个看你个人 ...
- python笔记3 闭包 装饰器 迭代器 生成器 内置函数 初识递归 列表推导式 字典推导式
闭包 1, 闭包是嵌套在函数中的 2, 闭包是内层函数对外层函数的变量(非全局变量)的引用(改变) 3,闭包需要将其作为一个对象返回,而且必须逐层返回,直至最外层函数的返回值 闭包例子: def a1 ...
- apt如何列出所有已经安装的软件包
apt如何列出所有已经安装的软件包 转 https://www.helplib.com/ubuntu/article_155294 问题: 我想将所有已安装软件包的列表输出到文本文件中,以便我可以查看 ...
- context_processor 上下文处理器
context_processor 上下文处理器 博客里面有三个地方用到了标签云:主页面,分类页面,博客详情页面,于是有了下面一段代码 # 主页面 @main.route("/") ...
- Redis查询_Tips
基础知识——介绍 Redis简介 REmote Dictionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个完全 ...
- java+断点续传
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...
- PAT 甲级 1019 General Palindromic Number (进制转换,vector运用,一开始2个测试点没过)
1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards ...
- 工具的更新换代 总是要折腾一下Windows10下Java Web 开发环境的配置
Windows10下Java Web 开发环境的配置 由于经常性遗忘,所以整理一下 Java Web 开发环境的搭建与配置,利人利己 主要分为以下几步,需要可以挑着看 Windows下 JDK 的下载 ...
- 微信小程序 仿‘得到app’分类列表页
今天另起一篇,贴出完整的代码,大概思路是左侧大分类列表,点击后联动右侧二级分类,及下面文章列表,点击二级分类也联动下面文章列表. 代码如下: <view class="page&quo ...