6. Kitchen Plates(枚举,暴力)
6. Kitchen Plates
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou are given 5 different sizes of kitchen plates. Each plate is marked with a letter A, B, C, D, or E. You are given 55 statements comparing two different plates, you need to rearrange the plates from smallest size to biggest size.
For example: the sizes of these plates


Input
The input consist of 55 lines. In each line there will be 33 characters, the first and last character will be either A, B, C, D, or E and the middle character will be either > or < describing the comparison between two plates sizes. No two plates will be equal.
Output
The output consist of 5 characters, the sorted order of balls from smallest to biggest plate. Otherwise, if the statements are contradicting print impossible. If there are multiple answers, print any of them
Examples
input
Copy
D>B A>D E<C A>B B>C
output
Copy
ECBDA
input
Copy
B>E A>B E>A C<B D<B
output
Copy
impossible
题意简单,五个盘子根据给出的大小关系进行排序。这是一道拓扑排序的题目,但因为数据量小,使用next_permutation()直接枚举。
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define TL ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); 5 int a[5],p[5],q[5],pos[5]; 6 char s[100]; 7 int main() 8 { 9 TL; 10 for(int i = 0;i < 5;i++) 11 { 12 cin >> s; 13 p[i] = s[0] -'A'; 14 q[i] = s[2] -'A'; 15 if(s[1]=='<') swap(p[i],q[i]); 16 a[i] = i + 1; 17 } 18 do 19 { 20 int flag = 1; 21 for (int i = 0; i < 5; i++) 22 { 23 pos[a[i]-1] = i; 24 } 25 for (int i = 0; i < 5; i++) 26 { 27 if(pos[p[i]] < pos[q[i]]) flag = 0; 28 } 29 if(flag) 30 { 31 for(int i = 0;i < 5;i++) 32 { 33 cout << char('A'+a[i]-1); 34 } 35 return 0; 36 } 37 } while (next_permutation(a,a+5)); 38 puts("impossible"); 39 return 0; 40 }

浙公网安备 33010602011771号