UVa-140 - Bandwidth
2015-07-20 00:12
225 查看
决定跳过数据结构那章,来看暴力求解法。暴力这章之前也看过,可是前几天做了一道POJ的暴力题,却没有想起来自己曾经在书上看过类似的暴力枚举解法,所以决定再仔细看一遍。感觉要是不刷题,只是单纯的看书的话,记不住啊。
书中也说了,可以剪枝(“发现两个结点的距离大于或等于k”,“如果在搜索到结点u时,u结点还有m个相邻点没有确定位置”),不过这只能跳过当前排列而已,对减少时间复杂度的意义不是太大,所以我就还是贴上自己原本的代码。
前几天买了侯捷的《STL源码剖析》,最近在研究STL,真好用,比如这个题直接用next_permutation,方便极了。
书中也说了,可以剪枝(“发现两个结点的距离大于或等于k”,“如果在搜索到结点u时,u结点还有m个相邻点没有确定位置”),不过这只能跳过当前排列而已,对减少时间复杂度的意义不是太大,所以我就还是贴上自己原本的代码。
前几天买了侯捷的《STL源码剖析》,最近在研究STL,真好用,比如这个题直接用next_permutation,方便极了。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxx=100; const int maxn=250; int main() { char s[maxx]={}; while(~scanf("%s",s)&&s[0]!='#') { int len=strlen(s); bool f[maxn]={}; bool mg[maxn][maxn]={}; char a,b; bool f1=0; char v[maxn]={}; int n=0; for(int i=0;i<len;i++) { if(s[i]==':') { f1=1; continue; } if(s[i]==';') { f1=0; continue; } if(!f[s[i]]) { v[n++]=s[i]; f[s[i]]=1; } if(f1==0) a=s[i]; else { b=s[i]; mg[a][b]=mg[b][a]=1; } } sort(v,v+n); char vans[maxn]={}; int ans=1e9; do { int tans=0; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) if(mg[v[i]][v[j]]) tans=max(tans,j-i); if(tans<ans) { strcpy(vans,v); ans=tans; } }while(next_permutation(v,v+n)); for(int i=0;i<n;i++) printf("%c ",vans[i]); printf("-> %d\n",ans); } }
相关文章推荐
- ZOJ 3352-3361||Monthly July 2010
- Windows Azure存储共享配置介绍
- 黑马程序员—IOS之C语言基础—循环结构与分支结构
- poj1251(prime()+读取字符bug)
- 用express、mongodb、nodejs开发简单的登陆
- hbase环境搭建,启动之后HMaster挂掉,或者是集群里,只启动了HMaster节点,HRegionServer节点没有启动的问题
- [LeetCode]Remove Linked List Elements
- ApplicationContext的三种实现方式以及在web.xml配置的两种方式
- [BZOJ1085][SCOI2005]骑士精神
- Picasso:一个强大的图片下载和缓存库
- C Language Study - gets , getchar & scanf
- 我想做一个合格的工程师
- Exchange Server 2013 公网发布疑难解答
- 猜拳小游戏过程以及感悟
- [BZOJ2456]mode
- Ninject依赖注入——构造函数、属性、方法和字段的注入
- 解决The current branch is not configured for pull No value for key branch.master.merge found in config
- FreeBSD 10添加Swap文件
- eclipse连接hbase异常KeeperErrorCode = ConnectionLoss for /hbase/meta-region-server
- hdu1072(bfs搜索退出的条件有变)