您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: