zoukankan      html  css  js  c++  java
  • CF Fox And Names (拓扑排序)

    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 siand 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).

    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

    拓扑排序第一题,注意特判一下下面的串是上面的串的前缀这种情况,直接输出Impossible了。

     1 #include <bits/stdc++.h>
     2 using    namespace    std;
     3 
     4 bool    G[30][30];
     5 int    IN[30];
     6 int    ANS[30];
     7 
     8 void    toposort(void);
     9 int    main(void)
    10 {
    11     int    n;
    12     char    name[105][105];
    13     bool    flag;
    14 
    15     cin >> n;
    16     for(int i = 0;i < n;i ++)
    17         cin >> name[i];
    18     for(int i = 0;i < n - 1;i ++)
    19     {
    20         for(int j = 0;name[i][j] && name[i + 1][j];j ++)
    21         {
    22             flag = false;
    23             if(name[i][j] != name[i + 1][j])
    24             {
    25                 if(!G[name[i][j] - 'a'][name[i + 1][j] - 'a'])
    26                 {
    27                     G[name[i][j] - 'a'][name[i + 1][j] - 'a'] = true;
    28                     IN[name[i + 1][j] - 'a'] ++;
    29                 }
    30                 flag = true;
    31                 break;
    32             }
    33         }
    34         if(!flag && strlen(name[i]) > strlen(name[i + 1]))
    35         {
    36             puts("Impossible");
    37             return    0;
    38         }
    39     }
    40     toposort();
    41 
    42     return    0;
    43 }
    44 
    45 void    toposort(void)
    46 {
    47     queue<int>    que;
    48     int    sum = 1;
    49     int    k = 0;
    50     int    front;
    51 
    52     for(int i = 0;i < 26;i ++)
    53         if(!IN[i])
    54         {
    55             ANS[k ++] = i;
    56             sum ++;
    57             que.push(i);
    58         }
    59 
    60     while(!que.empty())
    61     {
    62         front = que.front();
    63         que.pop();
    64         for(int i = 0;i < 26;i ++)
    65             if(G[front][i])
    66             {
    67                 IN[i] --;
    68                 if(!IN[i])
    69                 {
    70                     ANS[k ++] = i;
    71                     que.push(i);
    72                     sum ++;
    73                 }
    74             }
    75     }
    76     if(sum < 26)
    77         puts("Impossible");
    78     else
    79     {
    80         for(int i = 0;i < 26;i ++)
    81             printf("%c",ANS[i] + 'a');
    82         puts("");
    83     }
    84 
    85     return    ;
    86 }
  • 相关阅读:
    ipython notebook
    使用gevent多线程下载豆瓣音乐 李少宏 博客园
    牛人
    Lucene中对document(记录)的CURD操作~为分布式全文检索设计
    DDD领域驱动设计(Domain Driven Design)(转)
    LINQtoSQL那点事~线程共享的DbContext与私有的DbContext
    LINQtoSQL那点事~LINQtoSQL中的数据缓存与应对
    分布式中的DTO(转)
    php处理checkbox
    filter_var()函数解释
  • 原文地址:https://www.cnblogs.com/xz816111/p/4452849.html
Copyright © 2011-2022 走看看