CF C. Fox And Names

C. Fox And Names
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 is always sorted in the lexicographical order.

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

She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.

Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: si ≠ ti. 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 si and tiaccording 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 are different.

Output

If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation 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

///这道题不是太难 加油做 应该可以做出来
///拓扑排序

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;
#define maxn 105
int N,n;
string s[maxn];
int go[27][27];
int topu[27];
int vis[27];

bool dfs(int u)
{
      vis[u]=-1;
    for(int i=0; i<26; i++)
        if(go[u][i])
    {
        if(vis[i]==-1) return false;
    else if(!vis[i]&&!dfs(i)) return false;
    }
    vis[u]=1;
    topu[--n]=u;
    return true;
}

int  cmp(string s1, string s2)
{
    int len1 = s1.size();
    int len2 = s2.size();
    if(!s2.compare(s1.substr(0,len2))) return 0;///如果后一个字符串属于前一个字符串
    else if(!s1.compare(s2.substr(0,len1))) return -1;
    else return 1;
}

bool solve()
{
    for(int i=0; i<N-1; i++)
    {
        int tmp=cmp(s[i],s[i+1]);
        if(!tmp)  return false;
        if(tmp==-1) continue;
        if(tmp==1)
        {
            int len=s[i].size();
            for(int k=0; k<len; k++)
            {
                int t1=s[i][k]-'a';
                int t2=s[i+1][k]-'a';
                if(t1!=t2)
                {
                    go[t1][t2]=1;
                    break;
                }
            }
        }
    }

    for(int u=0; u<26; u++)
        if(!vis[u])
        {
            if(!dfs(u)) return false;
        }
    return true;
}

int main()
{
    while(cin>>N)
    {
        n=26;
        ///memset(topu,0,sizeof(topu));
        memset(vis,0,sizeof(vis));
        memset(go,0,sizeof(go));
        for(int i=0; i<N; i++)
            cin>>s[i];
        bool ans=solve();
        if(!ans)
            cout<<"Impossible"<<endl;
        else
        {
                for(int i=0; i<26; i++)
                {
                    char c='a'+topu[i];
                    cout<<c;
                }
            cout<<endl;
        }
    }
    return 0;
}


你可能感兴趣的:(ACM,codeforces)