#include<stdio.h>
#include<string.h>
int gra[30][30];
int node[30];
int numnd;
int ndpos[30];
int order[30];
int minwid;
int ans[30];
char input[300];
void dfs(int cur,int max)
{
if(cur == numnd)
{
if(max < minwid)
{
minwid = max;
memcpy(ans, order, sizeof(ans));
}
return;
}
for(int i=0; i < 30; ++i)
{
if(node[i] == 1) //in the graph
{
node[i] = 2; //in the order
order[cur] = i;
ndpos[i] = cur;
int v;
int imax = max ;
int dist;
for(v=0; v < 30; ++v)
{
if(gra[i][v] && node[v]==2)
{
dist = cur-ndpos[v];
imax = imax>dist ? imax:dist;
}
}
if(imax < minwid)
{
dfs(cur+1, imax);
}
node[i] = 1;
}
}
}
int main()
{
//
freopen("input.txt","r",stdin);
while(scanf("%s", input) == 1)
{
if(!strcmp(input,"#"))
break;
char* pt = input;
char* maohao;
char* fenhao;
bool loop = true; //go on
int u;
memset(gra, 0, sizeof(gra));
memset(node, 0, sizeof(node));
while(loop)
{
maohao = strchr(pt,':');
fenhao = strchr(pt,';');
if(!fenhao)
{
loop = false;
fenhao = input+strlen(input);
}
u = *(maohao-1)-'A';
node[u] = 1;
++maohao;
while(maohao < fenhao)
{
gra[u][*maohao-'A']=gra[*maohao-'A'][u]=1;
node[*maohao-'A'] = 1;
++maohao;
}
pt = fenhao+1;
}
numnd = 0;
for(int i=0; i < 30; ++i)
{
if(node[i])
numnd++;
}
minwid = 1000000;
dfs(0,0);
for(int i=0; i < numnd; ++i)
{
printf("%c ",ans[i]+'A');
}
printf("-> %d\n", minwid);
}
}
UVa140宽带
最新推荐文章于 2021-05-19 16:03:28 发布