没有清空向量导致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. C# 简单线程实例

    1.简单线程实例 以及委托(同步委托.异步委托) using System; using System.Collections.Generic; using System.Linq; using Sy ...

  2. HTTP 协议报文解析

    说明转载自https://blog.csdn.net/chf1142152101/article/details/74162755 本篇主要是为了记录HTTP中报文的格式,以便针对报文进行解析.首先会 ...

  3. python3爬虫二

    1.获取列表页文章url集合: scrapy shell http://blog.jobbole.com/all-posts/ response.css('div.post-meta a.archiv ...

  4. 【API】API函数创建用户,添加到管理组

    1 学习目标 使用API添加用户可以绕过某些杀毒软件的限制. 2 编程思路 2.1 代码原理 使用NetUserAdd这个API添加普通权限的用户,NetLocalGroupAddMembers这个A ...

  5. ioremap 与 mmap【转】

    转自:http://blog.csdn.net/junllee/article/details/7415732 内存映射 对于提供了MMU(存储管理器,辅助操作系统进行内存管理,提供虚实地址转换等硬件 ...

  6. DataSnap ClientdataSet 三层中主从表的操作

    非原创  摘自:http://hi.baidu.com/yagzh2000/blog/item/fc69df2cb9845de78b139946.html三层中主从表的操作(删除.新增.修改)一定要在 ...

  7. C# 将任意对象快速转换为Json

    由于最近项目里面Model类特别多,而我需要编写所有数据交互的接口,传输的格式是json,以前都是通过循环List<T>中的对象向StringBuilder对象中Apped字符串生成jso ...

  8. 如何消除手机设置的字体大小对Cordova app(Android)界面font-size的影响

    ===================== 更新分割线 =================== 现在发现其实不需要用安卓编辑器打开,也能找到这个文件,路径是platforms\android\Cord ...

  9. Qt Ubuntu 编译出错-1: error: 找不到 -lGL

    安装好,编译界面程序出错“-1: error: 找不到 -lGL” 在终端运行如下命令(安装Qt5.8.0) sudo apt-get install libqt5-dev sudo apt-get ...

  10. 不同系统与程序修改java.library.path的位置(转)

    原文地址:http://blog.csdn.net/quqibing001/article/details/51201768 Linux环境 系统变量LD_LIBRARY_PATH来添加Java.li ...