标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4902 | Accepted: 1982 |
Description
Input
Output
Sample Input
a b f g a b b f v w x y z v y x v z v w v
Sample Output
abfg abgf agbf gabf wxzvy wzxvy xwzvy xzwvy zwxvy zxwvy
Source
1 /* 2 Problem: 3 OJ: 4 User: S.B.S. 5 Time: 6 Memory: 7 Length: 8 */ 9 #include<iostream> 10 #include<cstdio> 11 #include<cstring> 12 #include<cmath> 13 #include<algorithm> 14 #include<sstream> 15 #include<queue> 16 #include<cstdlib> 17 #include<iomanip> 18 #include<cassert> 19 #include<climits> 20 #include<functional> 21 #include<bitset> 22 #include<vector> 23 #include<list> 24 #include<map> 25 #define F(i,j,k) for(int i=j;i<=k;i++) 26 #define M(a,b) memset(a,b,sizeof(a)) 27 #define FF(i,j,k) for(int i=j;i>=k;i--) 28 #define maxn 10001 29 #define inf 0x3f3f3f3f 30 #define maxm 1001 31 #define mod 998244353 32 //#define LOCAL 33 using namespace std; 34 int read(){ 35 int x=0,f=1;char ch=getchar(); 36 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 37 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 38 return x*f; 39 } 40 int n,m; 41 int a[maxn],d[maxn]; 42 int pos[maxn],cnt[maxn][2]; 43 bool vis[maxn]; 44 inline void dfs(int u) 45 { 46 if(u>n){ 47 F(i,1,n) cout<<(char)a[i]; 48 cout<<endl; 49 return; 50 } 51 F(i,1,n){ 52 if(!vis[i]){ 53 a[u]=d[i]; 54 pos[a[u]]=u; 55 vis[i]=true; 56 bool flag=true; 57 for(int j=1;j<=m&&flag;j++) 58 { 59 int aa=cnt[j][0],bb=cnt[j][1]; 60 if(pos[aa]==0||pos[bb]==0||pos[aa]<pos[bb]); 61 else flag=false; 62 } 63 if(flag) dfs(u+1); 64 pos[a[u]]=0; 65 vis[i]=false; 66 } 67 } 68 } 69 int main() 70 { 71 // std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y; 72 #ifdef LOCAL 73 freopen("data.in","r",stdin); 74 freopen("data.out","w",stdout); 75 #endif 76 string s; 77 istringstream ss; 78 char aa,bb,cc; 79 while(getline(cin,s)) 80 { 81 M(vis,0);M(pos,0); 82 n=m=0;ss.clear(); 83 ss.str(s); 84 while(ss>>cc) d[++n]=cc; 85 sort(d+1,d+n+1); 86 getline(cin,s); 87 ss.clear(); 88 ss.str(s); 89 while(ss>>aa>>bb){ 90 cnt[++m][0]=aa; 91 cnt[m][1]=bb; 92 } 93 dfs(1); 94 cout<<endl; 95 } 96 return 0; 97 }
标签:
原文地址:http://www.cnblogs.com/SBSOI/p/5910209.html