POJ 1094 Sorting It All Out

Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 22106 Accepted: 7616

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.

Source

今天下午做题有点困,不在状态,这题目调了很长时间才调出来,一个简单的拓扑排序,但是要考虑各种情况,这是一道联系逻辑思维的好题目,在做题的时候必须要读懂题目,如果根据前面几组的数据确定了大小关系那么以后即使出现矛盾的也不用管。如果出现了环,立即确定矛盾就好了,最后才判断序列是不是被确定
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
class num
{
    public:
    int end;
    int next;
}a[1000000];
int b[30],res[30],status[30],top1,statck[30],sum;
int in[30],out[30],n;
string s1[100000];
int main()
{
    int deal();
    int i,j,m,s,t,x,k;
    int c1,c2;
    while(cin>>n>>m)
    {
        if(!n&&!m)
        {
            break;
        }
        for(i=0;i<=m-1;i++)
        {
            cin>>s1[i];
        }
        for(i=0;i<=m-1;i++)
        {
            memset(in,0,sizeof(in));
            memset(out,0,sizeof(out));
            memset(b,-1,sizeof(b));
            memset(status,0,sizeof(status));
            sum=0;
            for(j=0,x=0;j<=i;j++)
            {
                c1=s1[j][0]-65; c2=s1[j][2]-65;
                if(!status[c1])
                {
                  status[c1]=1;
                  statck[sum++]=c1;
                }
                if(!status[c2])
                {
                    status[c2]=1;
                    statck[sum++]=c2;
                }
                in[c2]++; out[c1]++;
                a[x].end=c2; a[x].next=b[c1];
                b[c1]=x; x++;
            }
            top1=0;
            k=deal();
            if(!k)
            {
                cout<<"Inconsistency found after "<<i+1<<" relations."<<endl;
                break;
            }else if(k==1)
            {
                memset(status,0,sizeof(status));
                for(j=0;j<=top1-1;j++)
                {
                    status[res[j]]=1;
                }
                for(j=0;j<=n-1;j++)
                {
                    if(!status[j])
                    {
                        break;
                    }
                }
                if(j==n)
                {
                    cout<<"Sorted sequence determined after ";
                    cout<<i+1<<" relations: ";
                    for(j=0;j<=n-1;j++)
                    {
                        cout<<(char)(res[j]+65);
                    }
                    cout<<"."<<endl;
                    break;
                }
            }
        }
        if(i==m)
        {
            cout<<"Sorted sequence cannot be determined."<<endl;
        }
    }
    return 0;
}
int deal()
{
    int i,j,base,top,k,x,s,k2;
    int queue[10000];
    base=top=0;
    k=0;k2=0;
    for(i=0;i<=sum-1;i++)
    {
        if(in[statck[i]]==0)
        {
            queue[top++]=statck[i];
            if(k==0)
            {
                k=1;
            }else if(k==1)
            {
                k2=1;
            }
        }
    }
    if(k==0)
    {
        return 0;
    }
    s=0;
    while(base<top)
    {
        x=queue[base++];
        res[top1++]=x; s+=1;
        k=0;
        for(j=b[x];j!=-1;j=a[j].next)
        {
            in[a[j].end]-=1;
            if(in[a[j].end]==0)
            {
                queue[top++]=a[j].end;
                if(k==0)
                {
                    k=1;
                }else if(k==1)
                {
                    k2=1;
                }
            }
        }
    }
    if(s==sum&&k2==0)
    {
        return 1;
    }else if(s<sum)
    {
        return 0;
    }else
    {
        return 2;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
未来社区的建设背景和需求分析指出,随着智能经济、大数据、人工智能、物联网、区块链、云计算等技术的发展,社区服务正朝着数字化、智能化转型。社区服务渠道由分散向统一融合转变,服务内容由通用庞杂向个性化、服务导向转变。未来社区将构建数字化生态,实现数据在线、组织在线、服务在线、产品智能和决策智能,赋能企业创新,同时注重人才培养和科研平台建设。 规划设计方面,未来社区将基于居民需求,打造以服务为中心的社区管理模式。通过统一的服务平台和应用,实现服务内容的整合和优化,提供灵活多样的服务方式,如推送式、订阅式、热点式等。社区将构建数据与应用的良性循环,提高服务效率,同时注重生态优美、绿色低碳、社会和谐,以实现幸福民生和产业发展。 建设运营上,未来社区强调科学规划、以人为本,创新引领、重点突破,统筹推进、整体提升。通过实施院落+社团自治工程,转变政府职能,深化社区自治法制化、信息化,解决社区治理中的重点问题。目标是培养有活力的社会组织,提高社区居民参与度和满意度,实现社区治理服务的制度机制创新。 未来社区的数字化解决方案包括信息发布系统、服务系统和管理系统。信息发布系统涵盖公共服务类和社会化服务类信息,提供政策宣传、家政服务、健康医疗咨询等功能。服务系统功能需求包括办事指南、公共服务、社区工作参与互动等,旨在提高社区服务能力。管理系统功能需求则涉及院落管理、社团管理、社工队伍管理等,以实现社区治理的现代化。 最后,未来社区建设注重整合政府、社会组织、企业等多方资源,以提高社区服务的效率和质量。通过建立社区管理服务综合信息平台,提供社区公共服务、社区社会组织管理服务和社区便民服务,实现管理精简、高效、透明,服务快速、便捷。同时,通过培育和发展社区协会、社团等组织,激发社会化组织活力,为居民提供综合性的咨询和服务,促进社区的和谐发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值