zoukankan      html  css  js  c++  java
  • Sorting It All Out

    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 <map>
    #include <queue>
    #include <cmath>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #define inf 10001
    using namespace std;
    int n,m;
    int mp[26][26];///记录两个点是否存在前后顺序
    int ok[26],unsure;///ok记录某个点前面是否有点 unsure记录顺序是否唯一
    char ans[27],x,y,ch;///ans记录唯一的序列 index为下标 flag记录第几步中断
    int index,flag;
    int topsort()///拓扑
    {
        queue<int> q;
        unsure = 0;///初始
        index = 0;///初始
        int vis[26] = {0};///当vis的值和ok相同就相当于这个点前面已经没有点了,可以进入ans序列
        for(int i = 0;i < n;i ++)
        {
            if(vis[i] == ok[i])q.push(i);
        }
        if(q.empty())return -1;///如果不存在这样的点 说明存在环
        while(!q.empty())
        {
            if(q.size() > 1)
            {
                unsure = 1;///存在多个点无前后确定关系
            }
            ans[index ++] = q.front() + 'A';///进入ans序列
            for(int i = 0;i < n;i ++)
            {
                if(mp[q.front()][i])
                {
                    vis[i] ++;///i前面的点少了一个 之所以用vis,是维护ok和mp数组不变保证每次都是完整的拓扑
                    if(vis[i] == ok[i])q.push(i);
                    else if(vis[i] > ok[i])return -1;//有环
                }
            }
            q.pop();
        }
        return 1;
    }
    int main()
    {
        while(cin>>n>>m&&(n + m))
        {
            index = 0;
            flag = -1;
            int d = 0;
            memset(mp,0,sizeof(mp));
            memset(ok,0,sizeof(ok));
            for(int i = 0;i < m;i ++)
            {
                cin>>x>>ch>>y;
                if(!mp[x - 'A'][y - 'A'])
                {
                    if(ch == '<')
                    {
                        mp[x - 'A'][y - 'A'] = 1;
                        ok[y - 'A'] ++;
                    }
                    else if(ch == '>')
                    {
                        mp[y - 'A'][x - 'A'] = 1;
                        ok[x - 'A'] ++;
                    }
                }
                if(d == 1 || d == -1)continue;
                d = topsort();
                if(d == 1)///排序进行完
                {
                    if(index == n)///n个点排好序了
                    {
                        if(unsure == 0)flag = i + 1,ans[index] = '';///序列唯一
                        else d = 0;//不唯一
                    }
                    else///有环
                    {
                        d = -1,flag = i + 1;
                    }
                }
                else if(d == -1)flag = i + 1;///有环
            }
            if(!d)printf("Sorted sequence cannot be determined.
    ");
            else if(d == 1)printf("Sorted sequence determined after %d relations: %s.
    ",flag,ans);
            else printf("Inconsistency found after %d relations.
    ",flag);
        }
    }
  • 相关阅读:
    mysql数据类型
    Hive Getting Started补充
    Hive安装
    HDFS High Availability Using the Quorum Journal Manager
    用DBContext (EF) 实现通用增删改查的REST方法
    Internet Explorer 10 administration IE10管理
    配置AD RMS及SharePoint 2013 IRM问题解决及相关资源
    SharePoint 2013 首页修改
    Status: Checked in and viewable by authorized users 出现在sharepoint 2013 home 页面
    添加AD RMS role时,提示密码不能被验证The password could not be validated
  • 原文地址:https://www.cnblogs.com/8023spz/p/7868620.html
Copyright © 2011-2022 走看看