Rare Order _拓扑_2018_2_23

本文介绍了一种解决特殊排序问题的算法,该算法能够从已排序的字符串列表中推断出一种未知语言的字母排序规则。通过分析相邻字符串间的差异,程序可以逐步构建出这种语言的字母表排序顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A rare book collector recently discovered a book written in an unfamiliar language that used the same characters as the English language. The book contained a short index, but the ordering of the items in the index was different from what one would expect if the characters were ordered the same way as in the English alphabet. The collector tried to use the index to determine the ordering of characters (i.e., the collating sequence) of the strange alphabet, then gave up with frustration at the tedium of the task.

You are to write a program to complete the collector's work. In particular, your program will take a set of strings that has been sorted according to a particular collating sequence and determine what that sequence is.

Input

The input consists of an ordered list of strings of uppercase letters, one string per line. Each string contains at most 20 characters. The end of the list is signalled by a line dio is the single character `#'. Not all letters are necessarily used, but the list will imply a complete ordering among those letters that are used.

Output

Your output should be a single line containing uppercase letters in the order that specifies the collating sequence used to produce the input data file.

Sample Input

XWY
ZX
ZXY
ZXW
YWWX
#

Sample Output

XZYW


#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
int main(){
	string a,b;
	bool o[26];
	memset(o,false,sizeof(o));
	vector<int> v[26];
	int d[26];
	memset(d,0,sizeof(d));
	for(cin>>a;cin>>b,b!="#";a=b){
		for(int k=0;k<a.size();k++)
		if(a[k]!=b[k]){
			v[a[k]-65].push_back(b[k]-65);
			d[b[k]-65]++;
			o[a[k]-65]=1;
			o[b[k]-65]=1;
			break;
		}
	}
	queue<int> q;
	for(int i=0;i<26;i++)
	if(o[i]&&!d[i])
	q.push(i);
	
	while(!q.empty()){
		int t=q.front();
		q.pop();
		putchar(t+65);
		for(auto u:v[t]){
			d[u]--;
			if(!d[u])q.push(u); 
		} 
	}puts("");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值