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.
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
const int ms=;
int n,m;
bool appear[ms];
char output[ms+];
int cnt[ms];
int tmp[ms];
vector<vector<char> > v;
int topo_sort(int s)
{
int i,j,k,flag=;
int total=,r=;
for(i=;i<n;i++)
tmp[i]=cnt[i];
while(s--)
{
total=;
for(i=;i<n;i++)
if(appear[i]&&tmp[i]==)
{
j=i;
total++;
}
if(total>=)
{
if(total>)
flag=;
for(i=;i<v[j].size();i++)
tmp[v[j][i]]--;
tmp[j]=-;
output[r++]=j+'A';
output[r]=;
}
else
return -;
}
if(flag)
return r;
return ;
}
int main()
{
int i,j,k,judge,det;
char str[];
while(scanf("%d%d",&n,&m)==&&(n+m))
{
judge=;
det=;
int sum=;
v.clear();v.resize(n);
memset(cnt,,sizeof(cnt));
memset(appear,false,sizeof(appear));
for(i=;i<=m;i++)
{
scanf("%s",str);
cnt[str[]-'A']++;
v[str[]-'A'].push_back(str[]-'A');
if(!appear[str[]-'A'])
{
sum++;
appear[str[]-'A']=;
}
if(!appear[str[]-'A'])
{
sum++;
appear[str[]-'A']=;
}
if(judge==)
{
det=topo_sort(sum);
if(det==-)
{
judge=-;k=i;
}
else if(det==n)
{
judge=;
k=i;
}
}
}
if(judge==-)
printf("Inconsistency found after %d relations.\n",k);
else if(judge==)
printf("Sorted sequence cannot be determined.\n");
else
printf("Sorted sequence determined after %d relations: %s.\n",k,output);
}
return ;
}

Sorting It All Out的更多相关文章

  1. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  2. 1306. Sorting Algorithm 2016 12 30

    1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...

  3. 算法:POJ1007 DNA sorting

    这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; im ...

  4. U3D sorting layer, sort order, order in layer, layer深入辨析

    1,layer是对游戏中所有物体的分类别划分,如UIlayer, waterlayer, 3DModelLayer, smallAssetsLayer, effectLayer等.将不同类的物体划分到 ...

  5. WebGrid with filtering, paging and sorting 【转】

    WebGrid with filtering, paging and sorting by Jose M. Aguilar on April 24, 2012 in Web Development A ...

  6. ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting 【转】

    ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting FEBRUARY 27, 2012 14 COMMENTS WebG ...

  7. poj 1007:DNA Sorting(水题,字符串逆序数排序)

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 80832   Accepted: 32533 Des ...

  8. ural 1252. Sorting the Tombstones

    1252. Sorting the Tombstones Time limit: 1.0 secondMemory limit: 64 MB There is time to throw stones ...

  9. CF#335 Sorting Railway Cars

    Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  10. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

随机推荐

  1. bzoj 1061 [Noi2008]志愿者招募(数学模型,MCMF)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1061 [题意] 雇人满足每天至少需要的人数. [思路一] Byvoid的题解 clic ...

  2. 【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?

    UVa11991 Easy Problem from Rujia Liu?  思路:  构造数组data,使满足data[v][k]为第k个v的下标.因为不是每一个整数都会出现因此用到map,又因为每 ...

  3. 机器学习——Logistic回归

    参考<机器学习实战> 利用Logistic回归进行分类的主要思想: 根据现有数据对分类边界线建立回归公式,以此进行分类. 分类借助的Sigmoid函数: Sigmoid函数图: Sigmo ...

  4. HDU5634 Rikka with Phi 线段树

    // HDU5634 Rikka with Phi 线段树 // 思路:操作1的时候,判断一下当前区间是不是每个数都相等,在每个数相等的区间上操作.相当于lazy,不必更新到底. #include & ...

  5. leetcode—Plus one

      1.题目描述 Given a number represented as an array of digits, plus one to the number. 2.解法分析 不要被常规思路限制住 ...

  6. 一个谷粉和3年的Google Reader重度使用者的碎碎念

    2013-03-14 上午看到Andy Rubin辞去Android业务主管职务.由Chrome及应用高级副总裁继任的新闻,还在想这会给Android带来什么,中午刷微博的时候就挨了当头一棒:Goog ...

  7. Hadoop-Map/Reduce实现实现倒排索引

    先来简单介绍一下什么是文档倒排索引 倒排索引是文档检索系统中最常见的数据结构,被广泛应用在全文搜索引擎上.主要用来存储某个单词(或词组)在一个文档或者一组文档中的存储位置的映射,即提供了一种根据内容来 ...

  8. Spark系列(六)Master注册机制和状态改变机制

    各组件的注册流程如下图: 注册机制源码说明: 入口:org.apache.spark.deploy.master文件下的receiveWithLogging方法中的case RegisterAppli ...

  9. Android JNI之C/C++层调用JAVA

    转载请声明:原文转自:http://www.cnblogs.com/xiezie/p/5930032.html 从C/C++层调用JAVA层代码步骤: 1. 在JAVA类中创建java方法和本地方法 ...

  10. HDU 4911 Inversion (逆序数 归并排序)

    Inversion 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/A Description bobo has a sequen ...