UVA 140(p196)----Bandwidth
来源:互联网 发布:淘宝删除销量和评价 编辑:程序博客网 时间:2024/06/11 17:46
#include<iostream>#include<cstdio>#include<string>#include<algorithm>#include<cstring>#include<map>#include<vector>using namespace std;const int INF=1e7;string st;int num,ansP[10];int p[10],pos[10];vector<int> g[10];char letter[10];void prepare(){ map<char,int> v; vector<char> tmp; for(int i=0; i<=10; i++) g[i].clear(); int l=st.length(); for(int i=0; i<l; i++) if(st[i]!=':'&&st[i]!=';') if(!v[st[i]]) { v[st[i]]=1; tmp.push_back(st[i]); } sort(tmp.begin(),tmp.end()); num=tmp.size(); for(int i=0; i<tmp.size(); i++) { letter[i+1]=tmp[i]; v[tmp[i]]=i+1; } int i=0; while(1) { if(st[i]==':') { int j=v[st[i-1]]; i++; while(st[i]!=';') { g[j].push_back(v[st[i]]); i ++; if(i>=l) break; } } i++; if(i>=l) break; }}void solve(){ for(int i=1; i<=num; i++) p[i]=i; int ans=INF; do { int tmp=0; for(int i=1; i<=num; i++) pos[p[i]]=i; for(int i=1; i<=num; i++) for(int j=0; j<g[i].size(); j++) tmp=max(tmp,abs(pos[i]-pos[g[i][j]])); if(tmp<ans) { memcpy(ansP,p,sizeof(p)); ans=tmp; } /* for(int i=1;i<=num;i++) cout<<p[i]<<" "; cout<<"\n";*/ } while(next_permutation(p+1,p+num+1)); for(int i=1; i<=num; i++) printf("%c ",letter[ansP[i]]); printf("-> %d\n",ans);}#define debuint main(){#ifdef debug freopen("in.in","r",stdin);#endif // debug while(cin>>st) { if(st=="#") break; prepare(); solve(); } return 0;}题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=76
0 0
- UVA 140(p196)----Bandwidth
- UVa 140 - Bandwidth
- uva 140 - Bandwidth
- UVa 140 - Bandwidth
- uva 140 - Bandwidth
- UVa 140 - Bandwidth
- UVA 140 - Bandwidth
- UVa 140 - Bandwidth
- UVA 140 Bandwidth
- uva 140 Bandwidth
- uva 140 Bandwidth
- UVa:140 Bandwidth
- UVa 140 - Bandwidth
- uva 140 - Bandwidth
- UVa 140 Bandwidth
- UVa 140 - Bandwidth
- uva 140 Bandwidth 带宽
- UVA - 140 Bandwidth
- UVA 129(p195)----Krypton Factor
- Linux Framebuffer驱动剖析之一—软件需求
- HTML+CSS慕课网学习总结(四)
- Mac下修改mysql字符集
- 理解PHP中会话控制
- UVA 140(p196)----Bandwidth
- Linux Framebuffer驱动剖析之二—驱动框架、接口实现和使用
- php中的==和===
- 动态创建标记
- iOS开发Debug之去掉tableView的headerview的粘性
- Edison 与 Android 手机蓝牙通信SPP(SERIAL PORT PROFILE)
- 【Android开发】Android跨进程通讯(AIDL)官方文档及官方Demo讲解
- UVA 208(p214)----Firetruck
- 初始化与清理(1)