Imagine that you have to prepare a problem set for the forthcoming programming contest and you have already chosen the problems you will use in it. Before you start writing problem statements, preparing tests, and writing author solutions, you must give code names to all the problems.
A code name is a string that uniquely identifies the problem. For example, instead of saying “problem about the cipher grille”, you can simply say “problem grille.”
The problems in a problem set are arranged according to the lexicographical order of their code names. However, the program committee wants to get a fixed order of the problems. For example, the easiest problem should be put on the first place so that all the teams will be able to find it, or the letter D can be assigned to a really Difficult problem. Therefore, to obtain some predefined order of the problems in the problem set, the program committee needs to carefully choose the code names. This is just what you have to do.
To make your task easier, the program committee proposed three variants of the code name for each of the n problems in the problem set. You only have to choose an appropriate variant for each problem.
Input
The first line contains the number n of problems in the problem set (1 ≤ n ≤ 16). The i-th of the following n lines contains three possible code names for the i-th problem. The variants are separated with a space. The last line contains a permutation of the numbers from 1 to n. This is the order in which the problems must be arranged in the problem set. Each code name consists of lowercase Latin letters and its length is at most 20. All the code names are different.
Output
Output n lines. The i-th line should contain the code name of the problem that will have number i in the problem set. If there are several possible answers, output any of them. If it is impossible to choose the code names as required, output “IMPOSSIBLE”.
Sample Input
input | output |
---|---|
11 cipher grille kamkohob names codenames codes newtests rejudge timus size volume summit watchmen braineater twosides solution random yesorno keywords subversion commands bosses shooting shaitan game strategy playgame mnemonic palindromes bestname eligibility rectangle rules 2 1 7 10 9 6 11 3 8 4 5 |
codenames grille keywords mnemonic playgame random rectangle rejudge shaitan volume watchmen |
3 problems in the first sample are ordered not randomly 1 2 3 |
IMPOSSIBLE |
/* 先对a[i],把所有s重新排序 再把每一行的三个数进行排序,从上到下逐个比较,符合则往下 */ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; char s[25][25][25]; char s1[25][25][25]; char s2[25][25]; char s3[25][25]; int main() { int a[25]; int n; scanf("%d", &n); getchar(); for(int i = 1; i <= n ;i++) scanf("%s%s%s",s[i][1], s[i][2], s[i][3]); //for(int i = 1; i <= n ;i++) // printf("%s %s %s ", s[i][1], s[i][2],s[i][3]); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= n ; i++){ strcpy(s1[i][1], s[a[i]][1]); strcpy(s1[i][2], s[a[i]][2]); strcpy(s1[i][3], s[a[i]][3]); } // for(int i = 1; i <= n ; i++){ // printf("%s %s %s ",s1[i][1],s1[i][2], s1[i][3]); // } char ss[25]; for(int i = 1; i <= n ; i++){ if(strcmp(s1[i][1], s1[i][2]) < 0){ if(strcmp(s1[i][1] , s1[i][3]) < 0) { if(strcmp(s1[i][2], s1[i][3]) < 0); else { strcpy(ss, s1[i][2]); strcpy(s1[i][2], s1[i][3]); strcpy(s1[i][3], ss); } } else { strcpy(ss, s1[i][3]); strcpy(s1[i][3] , s1[i][2]); strcpy(s1[i][2], ss); strcpy(ss, s1[i][1]); strcpy(s1[i][1], s1[i][2]); strcpy(s1[i][2], ss); } } else { if(strcmp(s1[i][1], s1[i][3]) > 0){ if(strcmp(s1[i][2],s1[i][3]) > 0 ){ strcpy(ss, s1[i][1]); strcpy(s1[i][1], s1[i][3]); strcpy(s1[i][3], ss); } else { // printf("%d ",i); // printf("%s %s %s ",s1[i][1], s1[i][2], s1[i][3]); strcpy(ss, s1[i][1]); strcpy(s1[i][1], s1[i][3]); strcpy(s1[i][3], ss); strcpy(ss, s1[i][2]); strcpy(s1[i][2], s1[i][1]); strcpy(s1[i][1], ss); } } else { strcpy(ss, s1[i][1]); strcpy(s1[i][1], s1[i][2]); strcpy(s1[i][2], ss); } } } /* for(int i = 1; i <= n ;i++){ for(int j = 1; j <= 3; j++) printf("%s ", s1[i][j]); puts(""); }*/ strcpy(ss, s1[1][1]); strcpy(s3[1], s1[1][1]); //printf("%s", ss); int j; int flag = 0; for(int i = 2; i <= n ; i++){ for( j = 1; j <= 3; j++){ if(strcmp(ss, s1[i][j]) < 0){ // printf("%s ", ss); strcpy(s3[i], s1[i][j]); strcpy(ss, s1[i][j]); break; } } if(j > 3) {flag = 1;break;} } if(flag == 1) printf("IMPOSSIBLE "); else { for(int i = 1; i <= n ; i++) printf("%s ",s3[i]); } return 0; }