没有清空向量导致debug了好久

这题难以下手  不知道怎么dfs

原来是用排序函数。

letter[n]=i;
id[i]=n++;

用来储存与设置标记十分巧妙

 for(;;)
{
while(s[p]!=':'&&p<n1)p++;
if(p==n1)break;
while(s[e]!=';'&&e<n1)e++; for(int i=p+;i<e;i++)
{
dad.push_back(id[ s[p-] ]);
son.push_back(id[ s[i] ]);
}
p++;e++;
}

输入方式是一个难点  lrj采用一个父节点对应一个子结点  方便后续的判断,p和e的使用要注意!

然后 设置pai数组用来排序  数组里先是第一个字典序0,1,2,3。。。n-1(对应的是之前被标记的字母,因为字母是从A按顺序排序的  所以这就是第一个字典序)  然后用 next_permutation函数来自动改变其字典序

同时  设置一个posi函数来储存各个位置  相当于位置下标  差即为距离

这两个数组的设置很值得学习

memcpy 快速保存答案。想起在之前有一道求旅行路径的深搜题可以用

这题很有价值   多打几遍

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std; vector<int>dad,son;
int main()
{
char s[];
int letter[];
int id[];
while(scanf("%s",s)==&&s[]!='#')
{ int n=;
for(char i='A';i<='Z';i++)
{
if(strchr(s ,i)!=NULL)
{
letter[n]=i;
id[i]=n++;
}
}
int p=;
int e=;
int n1=strlen(s);
dad.clear();son.clear();
for(;;)
{
while(s[p]!=':'&&p<n1)p++;
if(p==n1)break;
while(s[e]!=';'&&e<n1)e++; for(int i=p+;i<e;i++)
{
dad.push_back(id[ s[p-] ]);
son.push_back(id[ s[i] ]);
}
p++;e++;
}
int pai[];
int posi[];
int bestposi[];
for(int i=;i<n;i++)pai[i]=i;
int ans=n;
do
{ for(int i=;i<n;i++)posi[ pai[i] ]=i;
int d=;
for(int i=;i<dad.size();i++)
d=max(d,abs( posi[dad[i]]-posi[ son[i] ] ) );
if(d<ans)
{
ans=d;
memcpy(bestposi,pai,sizeof(pai));
} }
while(next_permutation(pai,pai+n)); for(int i=;i<n;i++)printf("%c ",letter[bestposi[i]]);
printf("-> %d\n",ans);
}
return ;
}

7-6 Bandwidth UVA140的更多相关文章

  1. uva140 - Bandwidth

    Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orderi ...

  2. UVa140 Bandwidth 小剪枝+双射小技巧+枚举全排列+字符串的小处理

    给出一个图,找出其中的最小带宽的排列.具体要求见传送门:UVa140 这题有些小技巧可以简化代码的编写. 本题的实现参考了刘汝佳老师的源码,的确给了我许多启发,感谢刘老师. 思路: 建立双射关系:从字 ...

  3. Uva140 Bandwidth 全排列+生成测试法+剪枝

    参考过仰望高端玩家的小清新的代码... 思路:1.按字典序对输入的字符串抽取字符,id[字母]=编号,id[编号]=字母,形成双射       2.邻接表用两个vector存储,存储相邻关系     ...

  4. UVA140 ——bandwidth(搜索)

    Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the ...

  5. UVa140 Bandwidth 【最优性剪枝】

    题目链接:https://vjudge.net/contest/210334#problem/F  转载于:https://www.cnblogs.com/luruiyuan/p/5847706.ht ...

  6. 递归回溯 UVa140 Bandwidth宽带

    本题题意:寻找一个排列,在此排序中,带宽的长度最小(带宽是指:任意一点v与其距离最远的且与v有边相连的顶点与v的距离的最大值),若有多个,按照字典序输出最小的哪一个. 解题思路: 方法一:由于题目说结 ...

  7. UVA-140 Bandwidth (回溯+剪枝)

    题目大意:求一个使带宽最小的排列和最小带宽.带宽是指一个字母到其相邻字母的距离最大值. 题目分析:在递归生成全排列的过程中剪枝,剪枝方案还是两个.一.当前解不如最优解优时,减去:二.预测的理想解不必最 ...

  8. uva 140 bandwidth (好题) ——yhx

     Bandwidth  Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orde ...

  9. Propagation of Visual Entity Properties Under Bandwidth Constraints

    1. Introduction The Saga of Ryzom is a persistent massively-multiplayer online game (MMORPG) release ...

随机推荐

  1. 18. Spring Boot 、注册Servlet三大组件Servlet、Filter、Listener

    由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件 public class MyServlet extends ...

  2. 使用eclipse创建web项目的项目图文步骤

    在eclipse中使用maven ① maven插件:eclipse内置 ②maven插件的设置: [1]installations:指定maven核心程序的位置,不建议使用自带的maven程序而应该 ...

  3. wireshark数据包分析

    最近有不少同事开始学习Wireshark,他们遇到的第一个困难就是理解不了主界面上的提示信息,于是跑来问我.问的人多了,我也总结成一篇文章,希望对大家有所帮助.Wireshark的提示可是其最有价值之 ...

  4. ll(ls -l) 列属性

    文件属性 文件数 拥有者 所属的group 文件大小 建档日期 文件名 drwx------ 2 Guest users 1024 Nov 21 21:05 Mail

  5. UML和模式应用4:初始阶段(4)--需求制品之用例模型模板示例

    1. 前言 UP开发包括四个阶段:初始阶段.细化阶段.构建阶段.移交阶段: UP每个阶段包括 业务建模.需求.设计等科目: 其中需求科目对应的需求制品包括:设想.业务规则.用例模型.补充性规格说明.词 ...

  6. oracle 远程导入导出(本地win)

    导出 exp hongtastock_account/hongtastock_account@192.168.1.22/orcl file=D:\hongta\hongtastock_account. ...

  7. 【转】C++ map的基本操作和使用

    1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响较小,除了那个操作节点,对其它的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...

  8. spring aop的五种通知类型

    昨天在腾讯课堂看springboot的视频,老师随口提问,尼玛竟然回答错了.特此记录! 问题: Spring web项目如果程序启动时出现异常,调用的是aop中哪类通知? 正确答案是: 异常返回通知. ...

  9. openwrt 添加 802.1x客户端njit

    1.修改feed的配置文件 feeds.conf.default 添加下面两句: src-svn njit https://github.com/liuqun/openwrt-clients/trun ...

  10. SIP 认证

    理解SIP的认证 1. 认证和加密    认证(Authorization)的作用在于表明自己是谁,即向别人证明自己是谁.而相关的概念是MD5,用于认证安全.注意MD5仅仅是个hash函数而已,并不是 ...