zoukankan      html  css  js  c++  java
  • CF510C 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 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 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 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
    Copy
    3
    rivest
    shamir
    adleman
    output
    Copy
    bcdefghijklmnopqrsatuvwxyz
    input
    Copy
    10
    tourist
    petr
    wjmzbmr
    yeputons
    vepifanov
    scottwu
    oooooooooooooooo
    subscriber
    rowdark
    tankengineer
    output
    Copy
    Impossible
    input
    Copy
    10
    petr
    egor
    endagorion
    feferivan
    ilovetanyaromanova
    kostka
    dmitriyh
    maratsnowbear
    bredorjaguarturnik
    cgyforever
    output
    Copy
    aghjlnopefikdmbcqrstuvwxyz
    input
    Copy
    7
    car
    care
    careful
    carefully
    becarefuldontforgetsomething
    otherwiseyouwillbehacked
    goodluck
    output
    Copy
    acbdefhijklmnogpqrstuvwxyz

    拓扑排序
     1 //2021-03-10 08:16:55
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <queue>
     6 #include <algorithm>
     7 using namespace std;
     8 
     9 const int M = 110;
    10 char map[M][M], ans[27];
    11 int in[M];
    12 bool ok, link_map[27][27];
    13 
    14 int n;
    15 
    16 void topsort(){
    17     priority_queue<int, vector<int>, greater<int> > q;
    18     for(int i = 0; i < 26; i++){
    19         if(!in[i]) q.push(i);
    20     }
    21     int flag = 0;
    22     int k = 0;
    23     while(!q.empty()){
    24         flag = q.top();
    25         q.pop();
    26         ans[k++] = flag + 'a';
    27         for(int i = 0; i < 26; i++){
    28             if(link_map[flag][i]){
    29                 in[i]--;
    30                 if(!in[i]){
    31                     q.push(i);
    32                 }
    33             }
    34         }
    35     }
    36     if(k < 26) ok = true;
    37     ans[26] = '';
    38 }
    39 
    40 int main(){
    41     scanf("%d", &n);
    42     for(int i=1; i<=n; i++){
    43         scanf("%s", map[i]);
    44     }
    45     ok = false;
    46     memset(in, 0, sizeof(in));
    47     memset(link_map, false, sizeof(link_map));
    48     for(int i = 1; i < n && !ok; i++){
    49         for(int j = 0; j < M; j++){
    50             if(map[i][j] == '') break;
    51             else if(map[i+1][j] == ''){
    52                 ok = true;
    53                 break;
    54             }
    55             if(map[i][j] == map[i+1][j]) continue;
    56             int u = map[i][j] - 'a';
    57             int v = map[i+1][j] - 'a';
    58             if(link_map[u][v]) break;
    59             link_map[u][v] = true;
    60             in[v]++;
    61             break;
    62         }
    63     }
    64     topsort();
    65     if(ok) printf("Impossible
    ");
    66     else printf("%s", ans);
    67 
    68 
    69     return 0;
    70 }
     
  • 相关阅读:
    推荐6款优秀的海外免费杀毒软件 附下载链接
    30个高质量的Psd设计文件分享
    45个设计师们不常见的html5和css3漂亮模板
    40个极简WordPress主题
    makfile中 override 与 +=联合作用的验证
    对 makefile 中 flavor 函数的学习体会
    GNU make manual 翻译( 一百六十七)
    对 makefile 中 override 优先级的学习体会
    GNU make manual 翻译( 一百六十五)
    GNU make manual 翻译( 一百六十六)
  • 原文地址:https://www.cnblogs.com/sineagle/p/14509444.html
Copyright © 2011-2022 走看看