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 ...
随机推荐
- dpkg 删除 百度网盘 程序
sudo dpkg -l baidu* 查询得到具体名字 sudo dpkg --purge baidunetdisk 解决
- 7 AOP
AOP:Aspect Oriented Programming 面向切面编程.AOP是对面向对象编程的一种补充,在运行时动态地将代码切入到类的指定方法.指定位置的编程思想.将不同的方法的同一位置抽象成 ...
- ubuntu下安装g++
主要来自ubuntu中文社区http://www.ubuntu.org.cn/support/documentation/doc/VMware 首选,确认你已经安装了build-essential程序 ...
- 记录学习Linux遇到的问题
shl@shl-tx:~$ ifconfig Command 'ifconfig' not found, but can be installed with: sudo apt install net ...
- 属性 每秒10万吞吐 并发 架构 设计 58最核心的帖子中心服务IMC 类目服务 入口层是Java研发的,聚合层与检索层都是C语言研发的 电商系统里的SKU扩展服务
小结: 1. 海量异构数据的存储问题 如何将不同品类,异构的数据统一存储起来呢? (1)全品类通用属性统一存储: (2)单品类特有属性,品类类型与通用属性json来进行存储: 2. 入口层是Java研 ...
- Mybatis 事物回滚最简单的操作方式
简单例子 //假设这是一个service类的片段 try{ //出现异常 } catch (Exception e) { e.printStackTrace(); //设置手动回滚 Transacti ...
- git clone https://chromium.googlesource.com/失败
一.现象 连接着vpn,网页上可以直接打开网站,但是使用terminal 执行git clone https://chromium.googlesource.com/xxxx时, 报错 ...
- [转]MySQL 中 You can't specify target table '表名' for update in FROM clause错误解决办法
原文链接:https://blog.csdn.net/qq_29672495/article/details/72668008
- JQ也要面向对象~在JQ中扩展静态方法和实例方法(jq扩展方法)
JQ也要面向对象,事实上,无论哪种开发语言,在开发功能时,都要把面向对象拿出来,用它的思想去干事,去理解事,面向对象会使问题简单化,清晰化,今天说两个概念“静态方法”与“实现方法”,这个在面向对象的语 ...
- HDU 1087 最大递增子序列
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...