sdnu 1031 字母排序(拓扑排序的利用)

原题链接:http://210.44.14.31/problem/show/1031


很明显是拓扑排序的利用。

注意事项:

1.记录下输入到第几个条件,产生矛盾或者已经排好序。

2.在有矛盾且M>=N-1的情况下,不能输出无法确定顺序。


代码如下(感觉写的不够简练,暂且这样了):

#include<iostream>
#include<queue>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
vector<char>abc[27];
queue<char>sortabc;
int abcsum[27];
int main()
{
	int N, M;
	while (cin >> N >> M&&(N!=0||M!=0))
	{
		memset(abcsum, 0, sizeof(abcsum));
		string str;
		int key = 0;		//记录下输入到第几个条件,产生矛盾或者已经排好序
		bool p = true;		//记录是否有矛盾
		bool q = true;		//记录是否已经排好序
		for (int h = 1; h <= M; h++)
		{
			cin >> str;
			if (M < N - 1) continue;	//要排好N个序列至少有N-1个条件
			if (str[0] >= 'a'&&str[0] <= 'z')
				str[0] -= 32;
			if (str[2] >= 'a'&&str[2] <= 'z')
				str[2] -= 32;
			if (str[1] == '<')
			{
				abc[str[0] - 'A'].push_back(str[2]);
				abcsum[str[2] - 'A']++;
			}
			else
			{
				abc[str[2] - 'A'].push_back(str[0]);
				abcsum[str[0] - 'A']++;
			}
			if (p)
			{
				int tempabcsum[27];
				bool u=true;			//当顺序无法确定时,标记一下
				while (!sortabc.empty())
					sortabc.pop();
				for (int i = 0; i < N; i++)
					tempabcsum[i] = abcsum[i];
				for (int i = 0; i < N; i++)
				{
					int kk = -1;
					for (int j = 0; j < N; j++)
					{
						if (tempabcsum[j] == 0)
						{
							if (kk == -1) kk = j;
							else u = false;
						}
					}
					if (kk == -1)
					{
						key = h;
						p = false;
						break;
					}
					tempabcsum[kk]--;
					sortabc.push(kk + 'A');
					for (unsigned int k = 0; k < abc[kk].size(); k++)
						tempabcsum[abc[kk][k] - 'A']--;
				}
				if (u&&q&&sortabc.size() == N)
				{
					q = false;
					key = h;
				}
			}
		}

		if (!q&&sortabc.size() == N)
		{
			cout << "Sorted sequence determined after " << key << " relations: ";
			while (!sortabc.empty())
			{
				cout << sortabc.front();
				sortabc.pop();
			}
			cout << "." << endl;
		}
		else if (!p)
		{
			cout << "Inconsistency found after " << key << " relations." << endl;
		}
		else
		{
			cout << "Sorted sequence cannot be determined." << endl;
		}
		while (!sortabc.empty())
			sortabc.pop();
		for (int i = 0; i < 26; i++)
		{
			while (!abc[i].empty())
			{
				abc[i].pop_back();
			}
		}
	}
	return 0;
}


Antique Comedians of Malidinesia would like to play a new discovered comedy of Aristofanes. Putting it on a stage should be a big surprise for the audience so all the preparations must be kept absolutely secret. The ACM director suspects one of his competitors of reading his correspondece. To prevent other companies from revealing his secret, he decided to use a substitution cipher in all the letters mentioning the new play. Substitution cipher is defined by a substitution table assigning each character of the substitution alphabet another character of the same alphabet. The assignment is a bijection (to each character exactly one character is assigned -- not neccessary different). The director is afraid of disclosing the substitution table and therefore he changes it frequently. After each change he chooses a few words from a dictionary by random, encrypts them and sends them together with an encrypted message. The plain (i.e. non-encrypted) words are sent by a secure channel, not by mail. The recipient of the message can then compare plain and encrypted words and create a new substitution table. Unfortunately, one of the ACM cipher specialists have found that this system is sometimes insecure. Some messages can be decrypted by the rival company even without knowing the plain words. The reason is that when the director chooses the words from the dictionary and encrypts them, he never changes their order (the words in the dictionary are lexicographically sorted). String a1a2 ... ap is lexicografically smaller than b1b2 ... bq if there exists an integer i, i <= p, i <= q, such that aj=bj for each j, 1 <= j < i and ai < bi. The director is interested in which of his messages could be read by the rival company. You are to write a program to determine that. Input Output Sample Input 2 5 6 cebdbac cac ecd dca aba bac cedab 4 4 cca cad aac bca bdac Sample Output abcde Message cannot be decrypted.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值