求偏序集中的极大元与极小元

本文介绍了一种通过统计顶点的入度和出度来求解偏序集中极小元与极大元的方法,并提供了一段C++实现代码。该方法适用于偏序集中的元素数目不超过20的情况。

1.题目描述:

求偏序集中的极大元与极小元

成绩 10 开启时间 2017年05月16日 星期二 08:00
折扣 0.8 折扣时间 2017年06月1日 星期四 23:55
允许迟交 关闭时间 2017年06月7日 星期三 23:55

输入

输入偏序集<A, £>A中的元素数不超过20个,分别用单个小写的英文字母表示。

输入的第一行给出A中的各个元素,两个相邻的元素之间用逗号隔开。

输入的第二行给出偏序关系£,用有序对的形式给出(只给出哈斯图中的满足覆盖的两个元素形成的有序对),如<a,b>,<c,a>等等,两个相邻的有序对之间用逗号隔开。

输出

输出A的极小元与极大元。

输出的第一行给出各个极小元,两个相邻元素之间用逗号隔开,输出的元素要求按照英文字母的自然顺序排列输出。

输出的第二行给出各个极大元,两个相邻元素之间用逗号隔开,输出的元素要求按照英文字母的自然顺序排列输出。

  测试输入关于“测试输入”的帮助 期待的输出关于“期待的输出”的帮助 时间限制关于“时间限制”的帮助 内存限制关于“内存限制”的帮助 额外进程关于“{$a} 个额外进程”的帮助
测试用例 1 以文本方式显示
  1. a,b,c,d↵
  2. <a,b>,<c,d>↵
以文本方式显示
  1. a,c↵
  2. b,d↵
1秒 64M 0

3.解题思路:

直接统计每个点的入度和出度就好了,如果入度为0则肯定是极小元,出度为0肯定是极大元。不过考虑到给出字母不按字典序的,因此考虑用map映射一下。

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
map<char, int> vis;
map<int, char> mp;
int in[26], out[26];
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	memset(in, 0, sizeof(in));
	memset(out, 0, sizeof(out));
	int cnt = 0;
	char ch;
	ch = getchar();
	do
	{
		if (isalpha(ch))
		{
			vis[ch] = cnt;
			mp[cnt++] = ch;
		}
		ch = getchar();
	} while (ch != '\n');
	bool first = 1;
	ch = getchar();
	do
	{
		if (isalpha(ch))
		{
			if (first)
			{
				out[vis[ch]]++;
				first = 0;
			}
			else
			{
				in[vis[ch]]++;
				first = 1;
			}
		}
		ch = getchar();
	} while (ch != '\n' && ch != EOF);
	first = 1;
	for (int i = 0; i < cnt; i++)
		if (!in[i])
		{
			if (first)
			{
				first = 0;
				printf("%c", mp[i]);
			}
			else
				printf(",%c", mp[i]);
		}
	puts("");
	first = 1;
	for (int i = 0; i < cnt; i++)
		if (!out[i])
		{
			if (first)
			{
				first = 0;
				printf("%c", mp[i]);
			}
			else
				printf(",%c", mp[i]);
		}
	puts("");
#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值