#290 (div.2) C. Fox And Names

1.题目描述:点击打开链接

2.解题思路:本题利用拓扑排序解决。本题要求出一个a~z的排列,使得所有名字按照这样的“字典序”是逐渐增加的。显然这里存在着字母之间的大小关系,容易联想到拓扑排序。

那么该如何来排序呢?先思考一下简单的情况,假设姓名s,t是相邻的两个名字,如果s是t的一个前缀,那么跳过即可;反之如果t是s的前缀,那么肯定是无解的。如果不是以上这种情况,那么首个不相同的位置处的两个字母就可以连一条边,最终构建出一个有向图。最后利用拓扑排序的模板,为26个英文字母从后向前排序即可。

3.代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

#define N 110
char s[N][N];
int g[26][26];
int c[26], topo[26], t;
int n;

bool dfs(int u)
{
	c[u] = -1;
	for (int v = 0; v < 26;v++)
	if (g[u][v])
	{
		if (c[v] < 0)return false;
		if (!c[v] && !dfs(v))return false;
	}
	c[u] = 1;
	topo[--t] = u;
	return true;
}

bool toposort()
{
	t = 26;
	memset(c, 0, sizeof(c));
	memset(topo, 0, sizeof(topo));
	for (int i = 25; i >=0;i--)//注意,一定要从后往前来寻找
	if (!c[i])
	if (!dfs(i))return false;
	return true;
}
int main()
{
	//freopen("t.txt", "r", stdin);
	while (~scanf("%d", &n))
	{
		memset(g, 0, sizeof(g));
		for (int i = 0; i < n; i++)
			scanf("%s", s[i]);
		for (int i = 0; i < n - 1; i++)
		{
			int l1 = strlen(s[i]);
			int l2 = strlen(s[i + 1]);
			int p = 0;
			while (p < min(l1, l2) && s[i][p] == s[i + 1][p])p++;
			if (p == l1&&l1 < l2)continue;//如果s[i]是s[i+1]的前缀,那么跳过
			if (p == l2&&l2 < l1){ puts("Impossible"); goto x1; }//反之,一定无解
			if (g[s[i][p] - 'a'][s[i + 1][p] - 'a'])continue;
			g[s[i][p] - 'a'][s[i + 1][p] - 'a'] = 1;//建立有向图
		}
		if (toposort())
		{
			for (int i = 0; i < 26; i++)
				printf("%c", topo[i] + 'a');
			puts("");
		}
		else puts("Impossible");
	x1:;
	}
	return 0;
}
时间: 2024-10-12 04:15:39

#290 (div.2) C. Fox And Names的相关文章

Codeforces Round #290 (Div. 2) C. Fox And Names 拓扑排序

C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: t

Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring" int r,c; char map[55][55]; int vis[55][55]; int mark; int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; int judge(int x,int y) { if(x<0||x>=r||y<0||y>

#290 (div.2) D. Fox And Jumping

1.题目描述:点击打开链接 2.解题思路:本题利用扫描与维护解决.根据题意,能够走到所有的格子,一定是挑选出来的牌的步数的最大公约是1,这点很好理解.因为ax+by=1意味着只要有a个x和b个y就可以凑出来步数1.这样以来,只需要利用map来存储所有的公约数对应的最小费用即可.初始时刻base[0]=0,接下来就是从前往后扫描一遍这n个数,然后依次更新base中的每一个最大公约是对应的最小费用即可. 3.代码: #define _CRT_SECURE_NO_WARNINGS #include<i

#290 (div.2) B. Fox And Two Dots

1.题目描述:点击打开链接 2.解题思路:本题利用DFS来解决.本题要求判断一个图中是否存在相同颜色的圈.显然需要利用DFS来寻找.那么该如何寻找呢?题目中已经告诉了我们如何判断一个圈.那么只用根据题意描述来写DFS即可.从没有搜索过的结点开始,每次都找与它相邻的且颜色相同的结点来扩展,此时为了防止重复扩展,需要在DFS参数列表中加上前驱结点.这样以来,一旦发现某一个结点曾经已经标记过,说明找到了一个圈.直接输出Yes并退出循环. 3.代码: #define _CRT_SECURE_NO_WAR

C. Fox And Names Codeforces Round #290 (Div. 2)

C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: t

CodeForces510 C. Fox And Names(拓扑排序)

题目链接:http://codeforces.com/problemset/problem/510/C C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer S

Codeforces Round #290 (Div. 2) 解题报告 A.B.C.D.

A - Fox And Snake 模拟. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h> using na

Codeforces Round #290 (Div. 2) b

/** * @brief Codeforces Round #290 (Div. 2) b * @file a.cpp * @author mianma * @created 2015/02/04 15:17 * @edited 2015/02/04 15:17 * @type brute * @note */ #include <fstream> #include <iostream> #include <string> #include <cstring>

Codeforces Round #290 (Div. 2) 拓扑排序

C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: t