標籤:des style blog http color os io strong
題目連結:啊哈哈,點我點我
題意是:
第一列給出所有的字母數,第二列給出一些先後順序。然後按字典序最小的方式輸出所有的可能性。。。
思路:
總體來說是拓撲排序,但是又很多細節要考慮,首先要按字典序最小的方式輸出,所以自然輸入後要對這些字母進行排列,然後就是輸入了,用scanf不能讀空格,所以怎麼建圖呢??設定一個變數判斷讀入的先後順序,那麼建圖完畢後,就拓撲排序了,那麼多種方式自然就是dfs回溯了。。那麼這個問題就得到瞭解決。。
題目:Following Orders
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 3800 |
|
Accepted: 1502 |
Description
Order is an important concept in mathematics and in computer science. For example, Zorn‘s Lemma states: ``a partially ordered set in which every chain has an upper bound contains a maximal element.‘‘ Order is also important in reasoning about the fix-point semantics of programs.
This problem involves neither Zorn‘s Lemma nor fix-point semantics, but does involve order.
Given a list of variable constraints of the form x < y, you are to write a program that prints all orderings of the variables that are consistent with the constraints.
For example, given the constraints x < y and x < z there are two orderings of the variables x, y, and z that are consistent with these constraints: x y z and x z y.
Input
The input consists of a sequence of constraint specifications. A specification consists of two lines: a list of variables on one line followed by a list of contraints on the next line. A constraint is given by a pair of variables, where x y indicates that x < y.
All variables are single character, lower-case letters. There will be at least two variables, and no more than 20 variables in a specification. There will be at least one constraint, and no more than 50 constraints in a specification. There will be at least one, and no more than 300 orderings consistent with the contraints in a specification.
Input is terminated by end-of-file.
Output
For each constraint specification, all orderings consistent with the constraints should be printed. Orderings are printed in lexicographical (alphabetical) order, one per line.
Output for different constraint specifications is separated by a blank line.
Sample Input
a b f ga b b fv w x y zv y x v z v w v
Sample Output
abfgabgfagbfgabfwxzvywzxvyxwzvyxzwvyzwxvyzxwvy
Source
Duke Internet Programming Contest 1993,uva 124
代碼為:
#include<cstdio>#include<iostream>#include<algorithm>#include<map>#include<cstring>using namespace std;const int maxn=26+2;char str[maxn],ans[maxn],apa[maxn];int in[maxn],gra[maxn][maxn],res;map<char,int>mp;void topo(int depth){ if(depth==res) { printf("%s\n",ans); return; } for(int i=0;i<res;i++) { if(in[i]==0) { --in[i]; ans[depth]=apa[i]; for(int j=0;j<res;j++) { if(gra[i][j]) --in[j]; } topo(depth+1); ++in[i]; for(int j=0;j<res;j++) { if(gra[i][j]) ++in[j]; } } }}int main(){ int flag,k,len; char temp1,temp2; while(gets(str)) { k=0; memset(ans,0,sizeof(ans)); memset(in,0,sizeof(in)); memset(gra,0,sizeof(gra)); len=strlen(str); for(int i=0;i<len;i++) if(str[i]>='a'&&str[i]<='z') apa[k++]=str[i]; sort(apa,apa+k); for(int i=0;i<k;i++) mp[apa[i]]=i; res=k; gets(str); len=strlen(str); for(int i=0;i<len;i++) if(str[i]>='a'&&str[i]<='z') { if(flag) { temp1=str[i]; flag=0; } else { temp2=str[i]; gra[mp[temp1]][mp[temp2]]=1; in[mp[temp2]]++; flag=1; } } topo(0); printf("\n"); } return 0;}