zoukankan      html  css  js  c++  java
  • UVA200 Rare Order 拓扑排序

    Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

    [Submit]   [Go Back]   [Status]  

    Description

     Rare Order 

    A rare book collector recently discovered a book written in an unfamiliar language that used the same characters as the English language. The book contained a short index, but the ordering of the items in the index was different from what one would expect if the characters were ordered the same way as in the English alphabet. The collector tried to use the index to determine the ordering of characters (i.e., the collating sequence) of the strange alphabet, then gave up with frustration at the tedium of the task.

    You are to write a program to complete the collector's work. In particular, your program will take a set of strings that has been sorted according to a particular collating sequence and determine what that sequence is.

    Input

    The input consists of an ordered list of strings of uppercase letters, one string per line. Each string contains at most 20 characters. The end of the list is signalled by a line with the single character `#'. Not all letters are necessarily used, but the list will imply a complete ordering among those letters that are used.

    Output

    Your output should be a single line containing uppercase letters in the order that specifies the collating sequence used to produce the input data file.

    Sample Input

    XWY
    ZX
    ZXY
    ZXW
    YWWX
    #

    Sample Output

    XZYW

         给定一个新的规则,输出新规则下的“字典序”。题中保证一定会合法。

      代码如下:

      1 #include <cstring>
    2 #include <cstdlib>
    3 #include <cstdio>
    4 #include <queue>
    5 using namespace std;
    6
    7 char ss[1000000][25];
    8
    9 int map[30][30], here[30], hash[30];
    10
    11 inline void sscmp( char *a, char *b )
    12 {
    13 int len1 = strlen( a ), len2 = strlen( b );
    14 int bound = len1 < len2 ? len1 : len2;
    15 for( int i = 0; i < bound; ++i )
    16 {
    17 if( a[i] != b[i] )
    18 {
    19 // printf( "%c %c\n", a[i], b[i] );
    20 map[ a[i] - 'A' ][ b[i] - 'A' ] = 1;
    21 here[ a[i] - 'A' ] = here[ b[i] - 'A' ] = 1;
    22 break;
    23 }
    24 }
    25 }
    26
    27 inline void search( int k )
    28 {
    29 for( int i = 0; i < k - 1; ++i )
    30 {
    31 sscmp( ss[i], ss[i + 1] );
    32 }
    33 }
    34
    35 int find()
    36 {
    37 for( int i = 0; i < 26; ++i )
    38 {
    39 if( here[i] && !hash[i] )
    40 {
    41 int j;
    42 for( j = 0; j < 26; ++j )
    43 {
    44 if( map[j][i] != 0 )
    45 break;
    46 }
    47 if( j == 26 )
    48 {
    49 return i;
    50 }
    51 }
    52 }
    53 return -1;
    54 }
    55
    56 void topsort( int rec[], int &cnt )
    57 {
    58 queue<int>q;
    59 int f = find();
    60 if( f != -1 )
    61 {
    62 hash[f] = 1;
    63 q.push( f );
    64 }
    65 else
    66 return;
    67 while( !q.empty() )
    68 {
    69 int pos = q.front();
    70 rec[cnt++] = pos;
    71 q.pop();
    72 int i;
    73 for( i = 0; i < 26; ++i )
    74 {
    75 map[pos][i] = 0;
    76 }
    77 f = find();
    78 if( f != -1 )
    79 {
    80 hash[f] = 1;
    81 q.push( f );
    82 }
    83 else
    84 return;
    85 }
    86 }
    87
    88 int main()
    89 {
    90 int k, rec[30], cnt = 0;
    91 for( k = 0; ; ++k )
    92 {
    93 scanf( "%s", ss[k] );
    94 if( ss[k][0] == '#' )
    95 break;
    96 }
    97 search( k );
    98 topsort( rec, cnt );
    99 for( int i = 0; i < cnt; ++i )
    100 printf( i == cnt - 1 ? "%c\n" : "%c", rec[i] + 'A' );
    101 }

      

  • 相关阅读:
    【网易官方】极客战记(codecombat)攻略-森林-边地僵局backwoods-standoff
    【网易官方】极客战记(codecombat)攻略-森林-Else 之战elseweyr
    【网易官方】极客战记(codecombat)攻略-森林-森林劈斩者woodland-cleaver
    【网易官方】极客战记(codecombat)攻略-森林-食人魔营地-ogre-encampment
    Invalid bound statement (not found) 找不到mapper 映射文件异常
    No compiler is provided in this environment. --Maven build失败
    mysql 无法链接, 输入密码失败
    Failed to read artifact descriptor for org.apache.maven.plugins:maven-install-plugin-JavaWeb(四)
    mysql连接报java.math.BigInteger cannot be cast to java.lang.Long异常
    添加日志(配置spring)---Java_web
  • 原文地址:https://www.cnblogs.com/Lyush/p/2170809.html
Copyright © 2011-2022 走看看