您的位置:首页 > 其它

poj 1270 Following Orders

2012-11-12 14:45 274 查看
FollowingOrders
TimeLimit:1000MSMemoryLimit:10000K
TotalSubmissions:2732Accepted:1041

Description
Orderisanimportantconceptinmathematicsandincomputerscience.Forexample,Zorn'sLemmastates:``apartiallyorderedsetinwhicheverychainhasanupperboundcontainsamaximalelement.''Orderisalsoimportantinreasoningaboutthefix-pointsemanticsofprograms.

ThisprobleminvolvesneitherZorn'sLemmanorfix-pointsemantics,butdoesinvolveorder.
Givenalistofvariableconstraintsoftheformx<y,youaretowriteaprogramthatprintsallorderingsofthevariablesthatareconsistentwiththeconstraints.

Forexample,giventheconstraintsx<yandx<ztherearetwoorderingsofthevariablesx,y,andzthatareconsistentwiththeseconstraints:xyzandxzy.

Input
Theinputconsistsofasequenceofconstraintspecifications.Aspecificationconsistsoftwolines:alistofvariablesononelinefollowedbyalistofcontraintsonthenextline.Aconstraintisgivenbyapairofvariables,wherexyindicatesthatx<y.

Allvariablesaresinglecharacter,lower-caseletters.Therewillbeatleasttwovariables,andnomorethan20variablesinaspecification.Therewillbeatleastoneconstraint,andnomorethan50constraintsinaspecification.Therewillbeatleastone,andnomorethan300orderingsconsistentwiththecontraintsinaspecification.

Inputisterminatedbyend-of-file.

Output
Foreachconstraintspecification,allorderingsconsistentwiththeconstraintsshouldbeprinted.Orderingsareprintedinlexicographical(alphabetical)order,oneperline.

Outputfordifferentconstraintspecificationsisseparatedbyablankline.

SampleInput

abfg
abbf
vwxyz
vyxvzvwv

SampleOutput

abfg
abgf
agbf
gabf

wxzvy
wzxvy
xwzvy
xzwvy
zwxvy
zxwvy

Source
DukeInternetProgrammingContest1993,uva124

//拓扑排序思想dfs输出所有情况

#include<iostream>
#include<stdio.h>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<math.h>
#include<string.h>
#include<algorithm>
usingnamespacestd;
vector<int>v[22];
charch[22];
intid[124];
intin[22];
boolbr[22];
charpt[22];
intn;
voiddfs(intdp)
{
if(dp==n){pt[dp]='\0';printf("%s\n",pt);return;}
inti,l,k;
for(i=0;i<n;i++)
{
if(br[i])continue;
if(in[i]==0)
{
pt[dp]=ch[i];
br[i]=1;
l=v[i].size();
for(k=0;k<l;k++)
in[v[i][k]]--;
dfs(dp+1);
br[i]=0;
for(k=0;k<l;k++)
in[v[i][k]]++;
}
}
}
intmain()
{
intf=0;
chara,b;
inti,k;
while(scanf("%c",&a)!=EOF)
{
n=0;
memset(in,0,sizeof(in));
memset(br,0,sizeof(br));
ch[n++]=a;
while(scanf("%c",&a),a!='\n')
{
if(a!='')
{
ch[n++]=a;
}
}
//printf("%d",n);
sort(ch,ch+n);
for(i=0;i<n;i++)
{
id[ch[i]]=i;
//printf("%c",ch[i]);
}
while(scanf("%c%c",&a,&b))
{
v[id[a]].push_back(id[b]);
in[id[b]]++;
//printf("%c%c",a,b);
if(scanf("%c",&a)==EOF)
return0;
//printf("%c",a);
if(a=='\n')break;
}
if(f)printf("\n");elsef=1;
dfs(0);
for(i=0;i<n;i++)v[i].clear();
}
return0;
}


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