Sorting It All Out

Sorting It All Out

Description

An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.

Input

Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character “<” and a second uppercase letter. No letter will be outside the range of

the first n letters of the alphabet. Values of n = m = 0 indicate end of input.

Output

For each problem instance, output consists of one line. This line should be one of the following three:

Sorted sequence determined after xxx relations: yyy…y. Sorted sequence cannot be determined. Inconsistency found after xxx relations.

where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy…y is the sorted, ascending sequence.

Sample Input

4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0

Sample Output

Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.



 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<map>
 6 #include<set>
 7 #include<stack>
 8 #include<queue>
 9 #include<vector>
10 using namespace std;
11 const int ms=26;
12 int n,m;
13 bool appear[ms];
14 char output[ms+1];
15 int cnt[ms];
16 int tmp[ms];
17 vector<vector<char> > v;
18 int topo_sort(int s)
19 {
20     int i,j,k,flag=1;
21     int total=0,r=0;
22     for(i=0;i<n;i++)
23         tmp[i]=cnt[i];
24     while(s--)
25     {
26         total=0;
27         for(i=0;i<n;i++)
28             if(appear[i]&&tmp[i]==0)
29             {
30                 j=i;
31                 total++;
32             }
33         if(total>=1)
34         {
35             if(total>1)
36                 flag=0;
37             for(i=0;i<v[j].size();i++)
38                 tmp[v[j][i]]--;
39             tmp[j]=-1;
40             output[r++]=j+'A';
41             output[r]=0;
42         }
43         else
44             return -1;
45     }
46     if(flag)
47         return r;
48     return 0;
49 }
50 int main()
51 {
52     int i,j,k,judge,det;
53     char str[4];
54     while(scanf("%d%d",&n,&m)==2&&(n+m))
55     {
56         judge=0;
57         det=0;
58         int sum=0;
59         v.clear();v.resize(n);
60         memset(cnt,0,sizeof(cnt));
61         memset(appear,false,sizeof(appear));
62         for(i=1;i<=m;i++)
63         {
64             scanf("%s",str);
65             cnt[str[2]-'A']++;
66             v[str[0]-'A'].push_back(str[2]-'A');
67             if(!appear[str[0]-'A'])
68             {
69                 sum++;
70                 appear[str[0]-'A']=1;
71             }
72             if(!appear[str[2]-'A'])
73             {
74                 sum++;
75                 appear[str[2]-'A']=1;
76             }
77             if(judge==0)
78             {
79                 det=topo_sort(sum);
80                 if(det==-1)
81                 {
82                     judge=-1;k=i;
83                 }
84                 else if(det==n)
85                 {
86                     judge=1;
87                     k=i;
88                 }
89             }
90         }
91         if(judge==-1)
92             printf("Inconsistency found after %d relations.\n",k);
93         else if(judge==0)
94             printf("Sorted sequence cannot be determined.\n");
95         else
96             printf("Sorted sequence determined after %d relations: %s.\n",k,output);
97     }
98     return 0;
99 }

 

转载于:https://www.cnblogs.com/767355675hutaishi/p/4078447.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109669.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • RabbitMQ Network Partitions 处理策略[通俗易懂]

    RabbitMQ Network Partitions 处理策略[通俗易懂]网络分区的意义RabbitMQ的模型类似交换机模型,且采用erlang这种电信网络方面的专用语言实现。RabbitMQ集群是不能跨LAN部署(如果要WAN部署需要采用专门的插件)的,也就是基于网络情况良好的前提下运行的。这种假设就好比paxos并不解决拜占庭问题。为什么RabbitMQ需要这种前提假设?这个它本身的数据一致性复制原理有关。RabbitMQ采用的镜像队列是一种环形的逻辑结构,…

    2022年6月26日
    23
  • iphone手机通过USB连接电脑,让电脑通过手机网络上网

    iphone手机通过USB连接电脑,让电脑通过手机网络上网1.iphone通过usb连接电脑后,用手机打开个人热点,如果不出现提示“只允许USB连接”提示框,那么把WIFI,蓝牙关掉,重新打开热点即可出现2。连接linux系统时,我的ubuntu14.04

    2022年7月2日
    68
  • python与pycharm区别_pycharm与anaconda

    python与pycharm区别_pycharm与anacondaipython和pycharm的区别:pycharm是一种pythonIDE,包含使用python语言开发时提高其效率的工具;ipython是一个python的交互式shell,内置了很多有用的功能和函数。PyCharm是一种PythonIDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试、语法高亮、Project管理、代码跳转、智能提示、自动完成、单元测试、版…

    2022年8月27日
    5
  • Java基础:volatile详解

    Java基础:volatile详解Java基础:volatile1、volatile保证可见性1.1、JMM模型的引入1.2、volatile保证可见性的代码验证1.2.1、无可见性代码验证1.2.1、volatile保证可见性验证2、volatile不保证原子性问:请谈谈你对volatile的理解?答:volatile是Java虚拟机提供的轻量级的同步机制,它有3个特性:1)保证可见性2)不保证原子性3)禁止指令重排刚学完java基础,如果有人问你什么是volatile?它有什么作用的话,相信一定非常懵逼…可能看了答案,也完

    2022年7月18日
    18
  • 进程的用户态和内核态的概念理解以及切换方法_用户进程从用户态切换到内核态

    进程的用户态和内核态的概念理解以及切换方法_用户进程从用户态切换到内核态原文链接:https://www.cnblogs.com/viviwind/archive/2012/09/22/2698450.html内核态:当一个任务(进程)执行系统调用而陷入内核代码中执行时,我们就称进程处于内核运行态。此时处理器处于特权级最高的(0级)内核代码中执行。当进程处于内核态时,执行的内核代码会使用当前进程的内核栈。用户态:每个进程都有自己的内核栈。当进程在执行用…

    2022年9月2日
    4
  • linux下svn清除非版本控制文件的方法

    linux下svn清除非版本控制文件的方法

    2021年10月20日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号