博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF Fox And Names (拓扑排序)
阅读量:6585 次
发布时间:2019-06-24

本文共 4222 字,大约阅读时间需要 14 分钟。

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: 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 inlexicographical 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:si ≠ ti. 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 siand ti 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 namei (1 ≤ |namei| ≤ 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).

Sample test(s)
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

 

 

拓扑排序第一题,注意特判一下下面的串是上面的串的前缀这种情况,直接输出Impossible了。

1 #include 
2 using namespace std; 3 4 bool G[30][30]; 5 int IN[30]; 6 int ANS[30]; 7 8 void toposort(void); 9 int main(void)10 {11 int n;12 char name[105][105];13 bool flag;14 15 cin >> n;16 for(int i = 0;i < n;i ++)17 cin >> name[i];18 for(int i = 0;i < n - 1;i ++)19 {20 for(int j = 0;name[i][j] && name[i + 1][j];j ++)21 {22 flag = false;23 if(name[i][j] != name[i + 1][j])24 {25 if(!G[name[i][j] - 'a'][name[i + 1][j] - 'a'])26 {27 G[name[i][j] - 'a'][name[i + 1][j] - 'a'] = true;28 IN[name[i + 1][j] - 'a'] ++;29 }30 flag = true;31 break;32 }33 }34 if(!flag && strlen(name[i]) > strlen(name[i + 1]))35 {36 puts("Impossible");37 return 0;38 }39 }40 toposort();41 42 return 0;43 }44 45 void toposort(void)46 {47 queue
que;48 int sum = 1;49 int k = 0;50 int front;51 52 for(int i = 0;i < 26;i ++)53 if(!IN[i])54 {55 ANS[k ++] = i;56 sum ++;57 que.push(i);58 }59 60 while(!que.empty())61 {62 front = que.front();63 que.pop();64 for(int i = 0;i < 26;i ++)65 if(G[front][i])66 {67 IN[i] --;68 if(!IN[i])69 {70 ANS[k ++] = i;71 que.push(i);72 sum ++;73 }74 }75 }76 if(sum < 26)77 puts("Impossible");78 else79 {80 for(int i = 0;i < 26;i ++)81 printf("%c",ANS[i] + 'a');82 puts("");83 }84 85 return ;86 }

 

转载于:https://www.cnblogs.com/xz816111/p/4452849.html

你可能感兴趣的文章
java finally块执行时机分析
查看>>
day6 字符串
查看>>
JMeter5.0 边界提取器使用
查看>>
Windows Azure 上的 Symfony,适用于 PHP 开发者的强大组合
查看>>
堆和栈的区别 (转贴)
查看>>
通过包名获取该包下的所有类
查看>>
【JavaScript学习笔记】画图
查看>>
Linux写时拷贝技术(copy-on-write)
查看>>
opencv视频读取问题
查看>>
java Iterator Fail-fast机制
查看>>
Java堆外内存之五:堆外内存管理类ByteBuffer
查看>>
HTML5 input placeholder 颜色修改
查看>>
TJ/T808 终端通讯协议设计与实现(码农本色)
查看>>
分布式搜索引擎Elasticsearch的查询与过滤
查看>>
SolidEdge 工程图中如何给零件添加纹理或贴图
查看>>
【Java面试题】14 super.getClass()方法调用
查看>>
六种流行的语言---C、C++、python、Java、php、C#比较[转]
查看>>
AP INVOICES IMPORT API(NOT request)
查看>>
怎样面试程序猿
查看>>
Redhat6.5安装DB2 Express-C版本
查看>>