UVA 140(p196)----Bandwidth
2016-02-25 22:30
423 查看
#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 debu int 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
相关文章推荐
- Servlet_01_介绍(续)
- IOS学习 查看UIBarButtonSystemItem 类型图片 / pageControl的使用
- 【JAVA】antlr.collections.AST.getLine()I异常
- UVA 129(p195)----Krypton Factor
- Linux Framebuffer驱动剖析之一—软件需求
- Linux Framebuffer驱动剖析之一—软件需求
- union和struct类型的大小计算
- 【解决方法】友盟分享 已安装QQ,但总提示“没有安装手机QQ客户端”
- Java String详解
- Linux Framebuffer驱动剖析之一—软件需求
- Struts2 自定义下拉框标签Tag
- Struts2 自定义下拉框标签Tag
- Struts2 自定义下拉框标签Tag
- UVA 120(p236)----Stacks of Flapjacks
- jquery json ajax -2
- leetcode-33-Search in Rotated Sorted Array
- 《leetCode》:Convert Sorted List to Binary Search Tree
- leetcode 316. Remove Duplicate Letters
- Sam版Matlab粒子群PSO工具已经更新
- 效率:条款19 了解临时对象的来源