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
原创粉丝点击