CF512A:Fox And Names(拓扑排序 & 字典树)

来源:互联网 发布:flash mac版下载 编辑:程序博客网 时间:2024/05/16 06:07

A. 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 in lexicographical 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).

Examples
input
3rivestshamiradleman
output
bcdefghijklmnopqrsatuvwxyz
input
10touristpetrwjmzbmryeputonsvepifanovscottwuoooooooooooooooosubscriberrowdarktankengineer
output
Impossible
input
10petregorendagorionfeferivanilovetanyaromanovakostkadmitriyhmaratsnowbearbredorjaguarturnikcgyforever
output
aghjlnopefikdmbcqrstuvwxyz
input
7carcarecarefulcarefullybecarefuldontforgetsomethingotherwiseyouwillbehackedgoodluck
output
acbdefhijklmnogpqrstuvwxyz
题意:给N个单词的排序,问是否能人为创造字母表顺序,使得这些单词就是按照该字母表的“字典序”排序。

思路:字典序就是在同一个位置,不同字母按字典序排,相同字母按下一位排,把单词扔到字典树建边跑一遍拓扑排序就行,注意a,aa这种也是Impossible的。

# include <bits/stdc++.h>using namespace std;char s[108];int in[28], vis[28], cnt=0, icount=0;int q[500], ans[28], id[28], flag=0;vector<int>v[28];struct node{int Next[28];}tri[30000];bool cmp(int x, int y){return ans[x] > ans[y];}void query(int len){    int cur = 0;    for(int i=0; i<len; ++i)    {        int c = s[i]-'a';        for(int j=0; j<26; ++j)        {            if(j!=c && tri[cur].Next[j])            {                ++in[j];                v[c].push_back(j);                if(++vis[c]==1) ++icount;                if(++vis[j]==1) ++icount;            }        }        if(i==len-1 && tri[cur].Next[c])        {            cur = tri[cur].Next[c];            for(int k=0; k<26; ++k) if(tri[cur].Next[k]) flag=1;        }        if(tri[cur].Next[c]) cur = tri[cur].Next[c];        else break;    }}void add(int len){    int cur = 0;    for(int i=0; i<len; ++i)    {        int c = s[i]-'a';        if(tri[cur].Next[c])            cur = tri[cur].Next[c];        else        {            tri[cur].Next[c] = ++cnt;            cur = cnt;        }    }}void solve(){    int l=0, r=0, _icount=0;    for(int i=0; i<26; ++i)        if(vis[i] && in[i] == 0)            q[r++] = i, ans[i] = 1, ++_icount;    while(l<r)    {        int s = q[l++];        for(auto e : v[s])        {            if(ans[e]){flag = 1;return;}            if(--in[e] == 0)            {                ans[e] = ans[s] + 1;                q[r++] = e;                ++_icount;            }        }    }    if(_icount != icount) flag = 1;}int main(){    int n;    scanf("%d",&n);    memset(tri, 0, sizeof(tri));    for(int i=0; i<n; ++i)    {        scanf("%s",s);        int len = strlen(s);        query(len);        if(flag) return 0*puts("Impossible");        add(len);    }    solve();    if(flag) return 0*puts("Impossible");    for(int i=0; i<26; ++i) id[i] = i;    sort(id, id+26, cmp);    for(int i=0; i<26; ++i)        printf("%c",id[i]+'a');    puts("");    return 0;}



阅读全文
0 0