Codeforces Round #290 (Div. 2) C. Fox and Names topological ordering

Source: Internet
Author: User

C. Fox and Namestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Fox Ciel is going to publish a paper on FOCS (Foxes operated computer Systems, pronounce: "Fox"). She heard a rumor:the authors list on the paper are always sorted in the lexicographical order.

After checking some examples, she found out of that sometimes it wasn ' t true. On some papers authors ' names weren ' t sorted inlexicographical order in normal sense. But it is always true this after some modification of the order of letters in alphabet, the order of authors becomes Lexicographical!

She wants to know, if there exists a order of letters in Latin alphabet such that the names on the paper she is Submittin G is following in the lexicographical order. If So, you should find off any such order.

lexicographicalOrder is defined in following. When we comparesandT, first we find the leftmost position with differing characters: si? ≠? T I . If There is no such position (I. E.sis a prefix ofTor vice versa) the shortest string is less. Otherwise, we compare characters si and Ti According to their order in alphabet.

Input

The first line contains an integer n (1?≤? N? ≤?100): Number of names.

Each of the followingNLines contain one string namei (1?≤?| name I|? ≤?100), theI-th name. Each name contains only lowercase Latin letters. All names is different.

Output

If there exists such order of letters that the given names is sorted lexicographically, output any such order as a Permut ation of characters ' a ' – ' Z ' (i. E. First output the first letter of the modified alphabet, then the second, and so on).

Otherwise output A single word "impossible" (without quotes).

Sample Test (s) input
3rivestshamiradleman
Output
Bcdefghijklmnopqrsatuvwxyz
Input
10touristpetrwjmzbmryeputonsvepifanovscottwuoooooooooooooooosubscriberrowdarktankengineer
Output
Impossible
Input
10petregorendagorionfeferivanilovetanyaromanovakostkadmitriyhmaratsnowbearbredorjaguarturnikcgyforever
Output
Aghjlnopefikdmbcqrstuvwxyz
Input
7carcarecarefulcarefullybecarefuldontforgetsomethingotherwiseyouwillbehackedgoodluck
Output
Acbdefhijklmnogpqrstuvwxyz
The topic gives a string set, and assumes that it is a dictionary order, requires 26-Letter dictionary (custom dictionary order), every two strings, you can derive the precedence of a pair of characters, and then the order of all the characters, then transformed into a topological sorting problem, using the priority queue, This can be as small as possible in front. Second, if there is ab a, such a string, can be directly considered impossible to exist!
 
#define N 105#define MOD 1000000000000000007struct node{int x;    Node (int xx) {x = xx;    } BOOL operator < (const Node A) const{return x>a.x; }};int N,in[30],ans[30],ansnum;char str[n][n];bool land[30][30];p riority_queue<node> myqueue;bool getland (int    X,int y) {int len = min (strlen (str[x]), strlen (Str[y]));            FI (len) {if (Str[x][i]! = Str[y][i]) {Land[str[x][i]-' A '][str[y][i]-' a '] = true;        return true;    }} if (Strlen (Str[x]) >strlen (Str[y])) return false; return true;}        int main () {while (S (n)!=eof) {FI (n) {SS (str[i]);        } memset (Land,false,sizeof (land));        BOOL flag = TRUE; for (int i=0;i<n && flag;i++) {for (int j = i+1;j<n && flag;j++) {flag = Get            Land (I,J);            }} if (!flag) {printf ("impossible\n");        Continue        } memset (In,0,sizeof (in)); FI (26) {FJ (+) {if (Land[i][j]) {in[j]++;        }}} while (!myqueue.empty ()) Myqueue.pop ();            for (int i=0;i<26;i++) {if (in[i] = = 0) {Myqueue.push (node (i));        }} ansnum = 0;            while (!myqueue.empty ()) {Node top = myqueue.top ();            Myqueue.pop ();            ans[ansnum++] = top.x;                    FI (+) {if (Land[top.x][i]) {in[i]--;                if (in[i] = = 0) Myqueue.push (node (i));            }}} if (Ansnum = =) {FI (ansnum) printf ("%c", ans[i]+ ' a ');        printf ("\ n");    } else printf ("impossible\n"); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces Round #290 (Div. 2) C. Fox and Names topological ordering

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.