CF Fox and Names (topological sort)

Source: Internet
Author: User

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 then the names on the paper she's is submit Ting is following in The lexicographical order. If So, you should find off any such order.

Lexicographical order is the defined in following. When we compare s and T, first we find the leftmost position with differing characters:s iti. If There is no such position (I. E. s is a prefix of T or vice versa) the shortest string is less. Otherwise, we compare characters siand ti according To their order in alphabet.

Input

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

Each of the following n lines contain one string namei (1≤| Namei|≤100), the i-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
3
Rivest
Shamir
Adleman
Output
Bcdefghijklmnopqrsatuvwxyz
Input
10
Tourist
Petr
Wjmzbmr
Yeputons
Vepifanov
Scottwu
Oooooooooooooooo
Subscriber
Rowdark
Tankengineer
Output
Impossible
Input
10
Petr
Egor
Endagorion
Feferivan
Ilovetanyaromanova
Kostka
Dmitriyh
Maratsnowbear
Bredorjaguarturnik
Cgyforever
Output
Aghjlnopefikdmbcqrstuvwxyz
Input
7
Car
Care
Careful
Carefully
Becarefuldontforgetsomething
Otherwiseyouwillbehacked
Goodluck
Output
Acbdefhijklmnogpqrstuvwxyz

Topological sequencing the first question, pay attention to special sentence The following string is the prefix of the above string, the direct output impossible.

1#include <bits/stdc++.h>2 using    namespacestd;3 4 BOOLg[ -][ -];5 intin[ -];6 intans[ -];7 8 voidToposort (void);9 intMainvoid)Ten { One     intN; A     Charname[ the][ the]; -     BOOLFlag; -  theCIN >>N; -      for(inti =0; i < N;i + +) -CIN >>Name[i]; -      for(inti =0; i < n-1; i + +) +     { -          for(intj =0; Name[i][j] && name[i +1][j];j + +) +         { AFlag =false; at             if(Name[i][j]! = name[i +1][j]) -             { -                 if(! G[NAME[I][J]-'a'][name[i +1][J]-'a']) -                 { -G[NAME[I][J]-'a'][name[i +1][J]-'a'] =true; -In[name[i +1][J]-'a'] ++; in                 } -Flag =true; to                  Break; +             } -         } the         if(!flag && strlen (name[i]) > strlen (name[i +1])) *         { $Puts"Impossible");Panax Notoginseng             return    0; -         } the     } + Toposort (); A  the     return    0; + } -  $ voidToposort (void) $ { -queue<int>que; -     intsum =1; the     intK =0; -     intFront;Wuyi  the      for(inti =0; I < -; i + +) -         if(!In[i]) Wu         { -Ans[k + +] =i; AboutSum + +; $ Que.push (i); -         } -  -      while(!que.empty ()) A     { +Front =Que.front (); the Que.pop (); -          for(inti =0; I < -; i + +) $             if(G[front][i]) the             { theIn[i]--; the                 if(!In[i]) the                 { -Ans[k + +] =i; in Que.push (i); theSum + +; the                 } About             } the     } the     if(Sum < -) thePuts"Impossible"); +     Else -     { the          for(inti =0; I < -; i + +)Bayiprintf"%c", Ans[i] +'a'); thePuts""); the     } -  -     return    ; the}

CF Fox and Names (topological sort)

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.