CodeForces510 C. Fox and Names (topological sort)

Source: Internet
Author: User
Tags in degrees

Title Link: Http://codeforces.com/problemset/problem/510/C


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


Test instructions

To find out if there is an alphabet, can satisfy the given string is from small to large given! If there is an output of this alphabet,

If not present, output impossible!

Ps:

22 Compare strings. If there are two strings with the same prefix, then the previous string is longer than the length of the last string, then it is definitely illegal! There will never be a legal alphabet!

Otherwise, the leftmost character is found and two nodes are formed, so that the previous character node points to the current character node, and then the topology is sorted.

The code is as follows:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < Queue>using namespace Std;int N;char s[147][147];int g[147][147];//Edge int in[147];//in degrees char ans[147];int k;void    Toposort ()//topology sort {Queue<int >q;    k = 0;            for (int i = 0; i < i++) {if (!in[i]) {Q.push (i);        ans[k++] = ' a ' + i;        }} while (!q.empty ()) {int f = q.front ();        Q.pop ();                for (int i = 0; i <; i++) {if (G[f][i])//have connected side {in[i]--;                    if (in[i] = = 0) {Q.push (i);                ans[k++] = ' a ' + i;    }}}} if (K <) {printf ("impossible\n");        } else {Ans[k] = ' + ';    printf ("%s\n", ans);        }}int Main () {while (~SCANF ("%d", &n)) {GetChar ();        memset (In,0,sizeof (in)); memset (g,0,sizeof (G));        for (int i = 0; i < n; i++) {gets (s[i]);        } int mark = 1;            for (int i = 0; i < n-1 && mark; i++) {int len1 = strlen (S[i]);            int len2 = strlen (s[i+1]);            int flag = 0;                for (int j = 0; J < len1 && J < Len2; J + +) {if (s[i][j]! = S[i+1][j]) {if (!                        g[s[i][j]-' A '][s[i+1][j]-' a ']) {g[s[i][j]-' a '][s[i+1][j]-' a '] = 1;                    in[s[i+1][j]-' a ']++;                    } flag = 1;                Break }} if (!flag && len1 > Len2)//special, the prefix is the same, and the string after the previous string is longer, certainly not valid {MA            RK = 0;            }} if (!mark) {printf ("impossible\n");        return 0;    } toposort (); } return 0;}


CodeForces510 C. 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.