拓扑排序入门

实际上挺早之前便了解了拓扑排序,但一直不是很清楚他有什么用的。最近做了一道拓扑排序的题目,大概对这类问题有了一些了解吧。拓扑排序适用于一种特殊的图,这种图有以下特征。
1:该图为有向图。
2:该图有严格的顺序,不存在后面的被指向的节点去指向前面的节点。
即不存在a->b->c->a这一条。
拓扑排序有两个作用,检查是否是拓扑图和输出拓扑序。
话不多少,例题来见
CodeForces - 510C
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: “Fox”). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.

After checking some examples, she found out that sometimes it wasn’t true. On some papers authors’ names weren’t sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!

She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.

Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s i ≠ t i. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s i and t i according to their order in alphabet.

Input
The first line contains an integer n (1 ≤ n ≤ 100): number of names.

Each of the following n lines contain one string name i (1 ≤ |name i| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.

Output
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters ‘a’–‘z’ (i. e. first output the first letter of the modified alphabet, then the second, and so on).

Otherwise output a single word “Impossible” (without quotes).

Examples
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz

这题的意思就是让我们重新排列字典序,然后判断是否解是合理的。什么是合理的呢,就是比如你a->b->c,后面不存在c->a的这种情况。代码有详解。

#include
#include
#include
using namespace std;
queue<int> q;
char ap[105][105];
int p[30][30];
queue<int> aov;
int vis[30];
int main()
{
	memset(ap,0,sizeof(ap));
	memset(p,0,sizeof(p));
	memset(vis,0,sizeof(vis));
	/*
		ap是读进去的字符串。
		p代表的是字母之间的关系,即图的关系。
		vis代表每个字母入度 
	*/
	int n;
	int pand=0;
	//有一种特殊情况要特判,就是如aeo在ae的前面的情况,就是前面字符串是比后面字符串长,而且两字符串共有部分一样。 
	scanf("%d",&n);
	for(int i=0;i<n;i++)scanf("%s",ap[i]);
	for(int i=0;i<n-1;i++)    //我们只要相邻的比就可以了,不用每个与后面所有的比,层次关系很明显的。 
	{
		for(int j=0;;j++)     //中间会有结束条件 
		{
			if(ap[i][j]!=ap[i+1][j])        //同位字母不同的情况 
			{
				if(ap[i+1][j]=='\0')pand=1;   //特判 
				else if(ap[i][j]=='\0');
				else if(p[ap[i][j]-'a'][ap[i+1][j]-'a']==0){ 
				 //a字母对应0,b字母对应1...防止产生a指向b多次被重复统计,因为下面拓扑排序时每次入度仅去1. 
					p[ap[i][j]-'a'][ap[i+1][j]-'a']=1;
					vis[ap[i+1][j]-'a']++;
				}
				break;
			}
		}
	}
	for(int i=0;i<26;i++)
	{
		if(vis[i]==0)
		{
			//拓扑排序第一步,入度为0进容器。 
			q.push(i);
		}
	}
	while(!q.empty())
	{
		int each=q.front();
		q.pop();
		aov.push(each);
		//拓扑排序第二步,取容器里的数放入拓扑序中,并消去其存在感。 
		for(int i=0;i<26;i++)
		{
			if(p[each][i]==1)
			{
				if(--vis[i]==0)q.push(i);
			}
		}
	}
	//判断是否是拓扑图以及特判 
	if(aov.size()!=26||pand==1)printf("Impossible");
	else
	{
	//输出拓扑序 
		while(!aov.empty())
		{
			printf("%c",aov.front()+'a');
			aov.pop();
		}
	}
	return 0;
}

你可能感兴趣的:(拓扑排序入门)