参考过仰望高端玩家的小清新的代码。。。

思路:1.按字典序对输入的字符串抽取字符,id[字母]=编号,id[编号]=字母,形成双射

      2.邻接表用两个vector存储,存储相邻关系

      3.先尝试字母编号字典序最小的排列,此为next_permutation的最上排列

      4.在最理想的情况下都不能得到比当前最优解更好的方案,则应当剪枝(prune)

      5.memcpy(),strchr()方法来自于库函数

测试集:

Input:

     A:FB;B:GC;D:GC;F:AGH;E:HD;

     #

Oput:

     A B C F G D H E -> 3

 //id[]输入字母的编号
//p[i]排列的字母编号 do{}先执行最小字典序的排列 #include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std; const int maxn=,inf=0x7fffffff;
char letter[maxn], s[];//字母,输入序列
int id[]; //字母的编号
int p[maxn]; //全排列的遍历数组 ,存储的是每个字母的编号
int pos[maxn];//记录每个字母的位置,避免频繁使用strchr int main(){
while(scanf(" %s",&s),s[]!='#'){
int len=strlen(s),n=;
for(char ch='A';ch<='Z';ch++)if(strchr(s,ch)!=NULL){
letter[n]=ch;
id[ch]=n++;
}
vector<int> u,v;
for(int i=;i<len;i++){
int t=i;//记录起始节点
i+=;
while(i<len && s[i]!=';'){
u.push_back(id[s[t]]);//加入起始节点
v.push_back(id[s[i]]);//加入起始节点的相邻节点
i++;
}
}
//遍历+剪枝
int bandwidth=,res=inf;
int bestP[maxn];//存储最终结果
for(int i=;i<n;i++)p[i]=i;
do{
bandwidth=;//初始化别忘了
for(int i=;i<n;i++)pos[p[i]]=i;//记录编号为pi的节点的位置
for(int i=;i<u.size();i++){
bandwidth=max(bandwidth,abs(pos[u[i]]-pos[v[i]]));
if(bandwidth>=res)break;//剪枝
}
if(bandwidth<res){
memcpy(bestP,p,sizeof(p));//memcpy比较快
res=bandwidth;
}
}while(next_permutation(p,p+n));
for(int i=;i<n;i++)printf("%c ",letter[bestP[i]]);
printf("-> %d\n",res);
}
}

Uva140 Bandwidth 全排列+生成测试法+剪枝的更多相关文章

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

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

  2. UVa140 Bandwidth 【最优性剪枝】

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

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

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

  4. 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 ...

  5. 【STL】全排列生成算法:next_permutation

    C++/STL中定义的next_permutation和prev_permutation函数是非常灵活且高效的一种方法,它被广泛的应用于为指定序列生成不同的排列. next_permutation函数 ...

  6. HDOJ-ACM1016(JAVA) 字典序全排列,并剪枝

    转载声明:原文转自http://www.cnblogs.com/xiezie/p/5576273.html 题意: 一个环是用图中所示的n个圆组成的.把自然数1.2.…….n分别放入每个圆中,并在相邻 ...

  7. 递归回溯 UVa140 Bandwidth宽带

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

  8. 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 ...

  9. 7_4 素数环(UVa524)<回溯法和生成-测试法的比较>

    有一个环(ring)是由n个圈圈所组成的(在这里n一定是个偶数),我们想要把1到n的自然数各放到一个圈圈中,使得相邻2个圈圈中的数的和一定是素数.下图为n=6的情形.请注意:第1个圈圈中的数一定是1. ...

随机推荐

  1. 并发编程(一):从头到脚解读synchronized

    一.目录 1.多线程启动方式 2.synchronized的基本用法 3.深度解析synchronized 4.同步方法与非同步方法是否能同时调用? 5.同步锁是否可重入(可重入锁)? 6.异常是否会 ...

  2. XManager5连接CentOS7

    XManager5连接CentOS6的方法已经行不通了,那么如何用XManager5连接CentOS7 从Xmanger官网博客得知: "Gnome in CentOS 7 tries to ...

  3. js数值使用及数组转json,转化后的json传入后台解析

    var storeArray=new Array(); $("input[name='storeid']").each(function(i){ var curStoreObj = ...

  4. sqlserver isnull判断

    --在新增或编辑的时候设置默认值或加isnull判断 Sql isnull函数 ISNULL(columName, 0)<>35 或 ISNULL(columName, '')<&g ...

  5. c# 读写文件时文件正由另一进程使用,因此该进程无法访问该文件

    c# 读写文件时文件正由另一进程使用,因此该进程无法访问该文件,在IO处理上遇到了无法操作的问题. 文件"D:\log.txt"正由另一进程使用,因此该进程无法访问该文件. log ...

  6. [leetcode-530-Minimum Absolute Difference in BST]

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  7. 【LeetCode】219. Contains Duplicate II

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  8. JAVA基础——Arrays工具类十大常用方法

    Arrays工具类十大常用方法 原文链接:http://blog.csdn.net/renfufei/article/details/16829457 0. 声明数组 String[] aArray ...

  9. date时间转换

    <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...

  10. Bootstrap警告框

    前面的话 在网站中,网页总是需要和用户一起做沟通与交流.特别是当用户操作上下文为用户提供一些有效的警示框,比如说告诉用户操作成功.操作错误.提示或者警告等.在Bootstrap框架有一个独立的组件,实 ...