UVa Rare Order: Topology sorting

Source: Internet
Author: User
Tags strlen time limit

200-rare Order

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=136

For a list of words, these words are arranged in an unknown dictionary order, which requires the order of the letters to be printed.

Idea: It's obvious that the topology sort.

Note that the last to output more than one line, otherwise it will wa!

Complete code:

/*0.016s*/#include <bits/stdc++.h> using namespace std;
List<int> l[27];
Char last[25], now[25];
BOOL has[27], vis[27];
  
int ans[27], CNT;
    void Dfs (int i) {vis[i] = true; if (L[i].size ()) for (List<int>::iterator iter = L[i].begin (); Iter!= l[i].end (); ++iter) if (!
    Vis[*iter]) DFS (*iter);
ans[cnt++] = i;
    int main () {int Len, I;
    Gets (last);
    HAS[LAST[0] &) = true;
        while (gets), now[0]!= ' # ' is {len = min (strlen (last), strlen (now)); for (i = 0; i < len; ++i) {if (Last[i]!= now[i]) {Has[last[i] &
                = Has[now[i] & = true;
                L[last[i] & 31].push_back (Now[i] & 31);
            Break
    } memcpy (last, now, sizeof (now));
    for (i = 1; i < ++i) if (Has[i] &&!vis[i]) DFS (i); for (i = cnt-1 i >= 0; i.) Putchar (Ans[i] + ' A '-1);
    Putchar (10);///tried to add a new line, AC, I was too young ...
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.