Sorting It All Out
Description
Input
the first n letters of the alphabet. Values of n = m = 0 indicate end of input.
Output
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的更多相关文章
- HDU Cow Sorting (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ...
- 1306. Sorting Algorithm 2016 12 30
1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...
- 算法:POJ1007 DNA sorting
这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; im ...
- U3D sorting layer, sort order, order in layer, layer深入辨析
1,layer是对游戏中所有物体的分类别划分,如UIlayer, waterlayer, 3DModelLayer, smallAssetsLayer, effectLayer等.将不同类的物体划分到 ...
- WebGrid with filtering, paging and sorting 【转】
WebGrid with filtering, paging and sorting by Jose M. Aguilar on April 24, 2012 in Web Development A ...
- 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 ...
- poj 1007:DNA Sorting(水题,字符串逆序数排序)
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 80832 Accepted: 32533 Des ...
- ural 1252. Sorting the Tombstones
1252. Sorting the Tombstones Time limit: 1.0 secondMemory limit: 64 MB There is time to throw stones ...
- CF#335 Sorting Railway Cars
Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
随机推荐
- 看仪表盘——validation
先试想一下,对于一个简单的二分类问题,我们如何选择合适的算法? 我们有许许多多的H,如何选择出最为合适的算法? 最合理的方法是:对于每一个H,我们选择出Eout最小的g,然后对于各个g,再选择Eout ...
- 七牛云存储官方接口PHP版本
PHP SDKv6 此 SDK 适用于 PHP 5.1.0 及其以上版本.基于 七牛云存储官方API 构建.使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上. ...
- Codeforces Round #363
http://codeforces.com/contest/699 ALaunch of Collider 题意:n个球,每个球向左或右,速度都为1米每秒,问第一次碰撞的时间,否则输出-1 贪心最短时 ...
- VS20xx下项目开发目录管理方法
在VS20XX之后项目管理使用解决方案(solution)管理一个大的开发工程中多个项目(Project). 以下目录配置与工程名称无关,适用于一般的工程组织过程. 对于一般的生成可执行程序的工程,使 ...
- leetcode—Valid Parentheses
1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...
- [算法] 希尔排序 Shell Sort
希尔排序(Shell Sort)是插入排序的一种,它是针对直接插入排序算法的改进.该方法又称缩小增量排序,因DL.Shell于1959年提出而得名. 希尔排序实质上是一种分组插入方法.它的基本思想是: ...
- 运行时环境(The Runtime Environment)
App Engine应用响应网络请求.当一个客户端(典型的是用户的Web浏览器)使用HTTP请求(比如获取在URL上的网页)连接上应用的时候,网络请求就开始了.当App Engine接收到请求时,它会 ...
- RabbitMQ>Erlang machine stopped instantly (distribution name conflict?). The service is not restarted as OnFail is set to ignore.-报错解决方案 原来是NNND。。。
>Erlang machine stopped instantly (distribution name conflict?). The service is not restarted as ...
- UVA 10054 The Necklace(欧拉回路,打印路径)
题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- cannot load such file -- openssl
[test@localhost usr]$ /usr/local/ruby/bin/gem install bundler ERROR: Loading command: install (LoadE ...