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的极小元与极大元。
输出的第一行给出各个极小元,两个相邻元素之间用逗号隔开,输出的元素要求按照英文字母的自然顺序排列输出。
输出的第二行给出各个极大元,两个相邻元素之间用逗号隔开,输出的元素要求按照英文字母的自然顺序排列输出。
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;
}
本文介绍了一种通过统计顶点的入度和出度来求解偏序集中极小元与极大元的方法,并提供了一段C++实现代码。该方法适用于偏序集中的元素数目不超过20的情况。

405

被折叠的 条评论
为什么被折叠?



