Codeforces Round #290 (Div. 2) C. Fox And Names 拓撲排序

來源:互聯網
上載者: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 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 andti 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 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
3rivestshamiradleman
output
bcdefghijklmnopqrsatuvwxyz
input
10touristpetrwjmzbmryeputonsvepifanovscottwuoooooooooooooooosubscriberrowdarktankengineer
output
Impossible
input
10petregorendagorionfeferivanilovetanyaromanovakostkadmitriyhmaratsnowbearbredorjaguarturnikcgyforever
output
aghjlnopefikdmbcqrstuvwxyz
input
7carcarecarefulcarefullybecarefuldontforgetsomethingotherwiseyouwillbehackedgoodluck
output
acbdefhijklmnogpqrstuvwxyz
題目給出一個字串集,並假設是字典序的,要求26個字母的字典序(自訂的字典序),每兩個字串,可以得出一對字元的優先順序,然後得出了所有字元的先後順序,就轉化成了拓撲排序的問題了,使用了優先隊列,這樣可以儘可能的小的在前面。其次,如果有ab a,這樣的字串,可以直接認為是不可能存在的!

#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];priority_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 = getland(i,j);            }        }        if(!flag){            printf("Impossible\n");            continue;        }        memset(in,0,sizeof(in));        FI(26){            FJ(26){                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(26){                if(land[top.x][i]){                    in[i]--;                    if(in[i] == 0)                    myqueue.push(node(i));                }            }        }        if(ansNum == 26){            FI(ansNum)                printf("%c",ans[i]+'a');            printf("\n");        }        else            printf("Impossible\n");    }    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Codeforces Round #290 (Div. 2) C. Fox And Names 拓撲排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.