URAL 1711. Code Names (数学啊 )

来源:互联网 发布:人过五十知天命感慨 编辑:程序博客网 时间:2024/04/24 13:23

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1711



1711. Code Names

Time limit: 0.5 second
Memory limit: 64 MB
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”.

Samples

inputoutput
11cipher grille kamkohobnames codenames codesnewtests rejudge timussize volume summitwatchmen braineater twosidessolution random yesornokeywords subversion commandsbosses shooting shaitangame strategy playgamemnemonic palindromes bestnameeligibility rectangle rules2 1 7 10 9 6 11 3 8 4 5
codenamesgrillekeywordsmnemonicplaygamerandomrectanglerejudgeshaitanvolumewatchmen
3problems in thefirst sample areordered not randomly1 2 3
IMPOSSIBLE



代码如下:

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;int main(){    int n;    string s[27][27];    while(~scanf("%d",&n))    {        for(int i = 0; i < n; i++)        {            for(int j = 0; j < 3; j++)            {                cin >> s[i][j];            }            sort(s[i],s[i]+3);//            for(int j = 0; j < 3; j++)//            {//                cout<<s[i][j]<<endl;//            }        }        int num;        string ans[27];        string tt="";        int flag = 0;        for(int i = 0; i < n; i++)        {            scanf("%d",&num);            num--;            if(i == 0)//最小的            {                tt = s[num][0];                ans[0] = tt;                continue;            }            //cout<<"tt:"<<tt<<endl;            int mark = 0;            for(int j = 0; j < 3; j++)            {                mark = 0;                if(s[num][j] > tt)                {                    mark = 1;                    tt = s[num][j];                    //cout<<"tt:"<<tt<<endl;                    ans[i] = s[num][j];                    break;                }            }            if(mark == 0)            {                flag = 1;            }        }        if(flag == 1)        {            printf("IMPOSSIBLE\n");            continue;        }        for(int i = 0; i < n; i++)        {            cout<<ans[i]<<endl;        }    }    return 0;}


1 0