题意:给出N个字符串判断这些字符串是否是按照某一个26个字母的排列的字典序排列的如果有输出这个序列
拓扑排序
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
using namespace std;
const int maxn=110;
char str[maxn][maxn];
int indgree[30];
bool map[30][30];
bool vis[maxn][maxn];
int ans[30];
bool topo(){
for(int i=0;i<26;++i){
int pos=-1;
for(int j=0;j<26;++j){
if(indgree[j]==0){
pos=j;break;
}
}
if(pos==-1)return false;
ans[i]=pos;indgree[pos]=-1;
for(int j=0;j<26;++j){
if(map[pos][j]){
indgree[j]--;
}
}
}
return true;
}
int main()
{
int n,i,j,k;
scanf("%d",&n);
for(i=1;i<=n;++i){
scanf("%s",str[i]+1);
}
bool sign=true;
for(i=1;i<=100;++i){
if(!sign)break;
for(j=1;j<=n;++j){
if(str[j][i]!=str[j-1][i]&&!vis[j][j-1]){
if(i<=strlen(str[j-1]+1)&&i>strlen(str[j]+1)){
sign=false;break;
}
if(i>strlen(str[j-1]+1)){
vis[j][j-1]=true;continue;
}
if(!map[str[j-1][i]-'a'][str[j][i]-'a'])
indgree[str[j][i]-'a']++;
map[str[j-1][i]-'a'][str[j][i]-'a']=true;
vis[j][j-1]=true;
}
}
}
if(!topo()||!sign){
printf("Impossible\n");
}
else {
for(i=0;i<26;++i){
printf("%c",ans[i]+'a');
}
}
printf("\n");
return 0;
}