*【CF#510C】Fox And Names (拓扑排序)

题干:

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: 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 si and 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).

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

解题报告:

    好难啊这题,,不知道为什么都觉得简单。。明明很难完全卡上拓扑排序的条件啊两个判断结束的条件,top不足26  或者两串每一个字符都分别相同并且后串比前串短。

 

AC代码:

#include<bits/stdc++.h>using namespace std;
char str[105][105];
int n;
int head[200],in[200];
int maze[200][200];
char ans[200];
int len[200];
bool vis[50][50];
int cnt,top;
struct Edge {int to,w,ne;
} e[400];
void add(int u,int v,int w) {e[cnt].to = v;e[cnt].ne = head[u];e[cnt].w = w;head[u] = cnt++;
}
bool topu() {//预处理 priority_queue< int , vector<int>, greater<int> > pq;//从小到大排 for(int i = 0; i<26; i++) {if(in[i] == 0 /*&& vis[i]*/) {
//			printf("i = %d\n",i);pq.push(i);
//			ans[++top] ='a'+i;}}while(!pq.empty() ) {int cur = pq.top();
//		printf("cur = %d\n",cur);pq.pop();ans[++top] = cur+'a';for(int i = head[cur]; i!=-1; i=e[i].ne) {//是ne啊!!!! in[e[i].to]--;	if(in[e[i].to] == 0 ) {pq.push(e[i].to);
//				ans[++top] = 'a' + e[i].to;		}}}if(top != 26) return false;else return true;
}
int main()
{memset(head,-1,sizeof(head));memset(vis,0,sizeof(vis));memset(in,0,sizeof(in));memset(maze,0,sizeof(maze) ) ;cnt = 0;cin>>n;//从str[1]开始读入字符串 for(int i = 1; i<=n; i++) {scanf("%s",str[i]);len[i] = strlen(str[i]);
//		vis[str[i][0] - 'a'] = 1;}int flag = 0;//点   从0到25; for(int i = 1; i<n; i++) {//是小于! flag = 0;for(int j = 0; j<min(len[i],len[i+1]); j++) {if(str[i][j] == str[i+1][j]) continue;flag = 1;if(maze[str[i][j]-'a'][str[i+1][j]-'a'] == 1) break;//写成continue了。。 add(str[i][j]-'a',str[i+1][j]-'a',0);maze[str[i][j]-'a'][str[i+1][j]-'a'] = 1;in[str[i+1][j]-'a'] ++;break;}if(flag == 0 && len[i] > len[i+1]) {printf("Impossible\n");return 0;} }if(topu()) {for(int i = 1; i<=26; i++) {printf("%c",ans[i]);}}else printf("Impossible\n");return 0 ;} 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441922.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java质,JAVA分解质因子 - osc_r1gtal48的个人空间 - OSCHINA - 中文开源技术交流社区

/*题目分解质因数(5分)题目内容&#xff1a;每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式&#xff0c;这几个素数就都叫做这个合数的质因数。比如&#xff0c;6可以被分解为2x3&#xff0c;而24可以被分解为2x2x2x3。现在&#xff0c;你的程序要读入一个[2,100…

【HDU - 5605】 geometry(水,数学题,推公式)

题干&#xff1a; There is a point PP at coordinate (x,y)(x,y). A line goes through the point, and intersects with the postive part of X,YX,Yaxes at point A,BA,B. Please calculate the minimum possible value of |PA|∗|PB||PA|∗|PB|. Input the first line…

matlab如何画函数的外包络曲线,怎样在MATLAB中划出一个函数的包络线?

沧海一幻觉下面是一系列关于MATLAB的包络线的程序&#xff1a;%这是定义了一个函数&#xff1a;function [up,down] envelope(x,y,interpMethod)%ENVELOPE gets the data of upper and down envelope of the known input (x,y).%% Input parameters:% x the abscissa of the g…

【51nod - 1087】 1 10 100 1000(找规律推公式,水,map)

题干&#xff1a; 1,10,100,1000...组成序列1101001000...&#xff0c;求这个序列的第N位是0还是1。 Input 第1行&#xff1a;一个数T&#xff0c;表示后面用作输入测试的数的数量。&#xff08;1 < T < 10000) 第2 - T 1行&#xff1a;每行1个数N。&#xff08;1 &…

php 异常错误信息用处,关于PHP中异常错误的处理详细介绍

1. 错误报告级别 error_reporting()error_reporting(int $level);PHP 5.4 及以上 E_ALL 包含了 E_STRICT。PHP Manual 所有的错误级别。范例&#xff1a;<?php // 关闭所有PHP错误报告error_reporting(0);// Report simple running errorserror_reporting(E_ERROR | E_WARN…

matlab计算流函数,hanyeah

上面的网址不知道什么时候就打不开了&#xff0c;赶紧保存一份&#xff0c;要不想看都看不到了。什么是流函数&#xff0c;什么是位函数(势函数)&#xff0c;可以自己搜索。说说我这里的应用场景。空间放一些电荷&#xff0c;我们能够算出任意一点的电场强度——一个矢量&#…

【51Nod - 1279】 扔盘子(思维)(on-p会超时)

题干&#xff1a; 有一口井&#xff0c;井的高度为N&#xff0c;每隔1个单位它的宽度有变化。现在从井口往下面扔圆盘&#xff0c;如果圆盘的宽度大于井在某个高度的宽度&#xff0c;则圆盘被卡住&#xff08;恰好等于的话会下去&#xff09;。 盘子有几种命运&#xff1a;1、…

java 内部类私有成员 能访问,为什么外部Java类可以访问内部类私有成员?

HUX布斯如果您想隐藏内部类的私有成员&#xff0c;您可以与公共成员定义一个接口&#xff0c;并创建一个实现此接口的匿名内部类。下面的例子&#xff1a;class ABC{private interface MyInterface{void printInt();}private static MyInterface mMember new MyInterface(){pr…

【HDU - 6290】 奢侈的旅行 (对题目预处理 + DIjkstra最短路)

题干&#xff1a; 高玩小Q不仅喜欢玩寻宝游戏&#xff0c;还喜欢一款升级养成类游戏。在这个游戏的世界地图中一共有nn个城镇&#xff0c;编号依次为11到nn。 这些城镇之间有mm条单向道路&#xff0c;第ii 条单项道路包含四个参数ui,vi,ai,biui,vi,ai,bi&#xff0c;表示一条…

数学建模matlab推荐,推荐数学建模matlab方法整理 - 图文

disp()函数的常见用法1、显示字符串 >> disp(sqrt(2)) sqrt(2)将要显示的字符串必须放在单引号里面&#xff01;&#xff01;&#xff01; 2、显示结果 >> disp(sqrt(2)) 1.41423、显示多个字符>> disp([sqrt(2),num2str(sqrt(2))]) sqrt(2)1.4142格式必须如…

【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)

题干&#xff1a; There is an apple tree outside of kakas house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The tree has N forks which are connected by branches. …

gjr garch Matlab,基于Copula-ARIMA-GJR-GARCH模型的股票指数相关性分析

摘要&#xff1a;当资产收益率分布较为复杂时,研究它们之间的相关关系就变得较为困难。特别是股票市场中资产的收益率分布非正态时,我们几乎不可能去精确模拟几种资产收益率之间的联合分布函数。然而Copula函数恰恰可以解决这类问题。Copula函数是用来描述随机变量间相依结构的…

【HDU - 3974】 Assign the task (dfs序 + 线段树维护 区间更新+ 单点查询)

题干&#xff1a; There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all hi…

php 插件 代码架构,php反射机制详以及插件架构实例详解

1。用途&#xff1a;该扩展分析php程序&#xff0c;导出或提取出关于类、方法、属性、参数等的详细信息&#xff0c;包括注释。Reflection可以说是对php库函数&#xff1a;“Classes/Objects 类&#xff0f;对象函数”的一个扩展。主要用在通过程序检测现有php程序内部关于类、…

【HDU - 1540】 Tunnel Warfare (线段树进阶操作 区间合并+ 单点更新+ 最长覆盖区间查询 )

题干&#xff1a; During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was …

php扩展包安装了为啥没加载,已安装PHP扩展但未加载

我正在尝试安装php的ssh2扩展,并且有一点点困难.文件在那里,它只是没有加载到PHP.首先,我安装了ssh2&#xff1a;aptitude install libssh2-1-dev libssh2-php(对于它的价值,我在Nginx上运行Ubuntu 12.04.)我可以看到使用modules命令加载ssh2&#xff1a;php -m |grep ssh2ssh2…

【HDU - 1166】敌兵布阵 (线段树模板 单点更新+ 区间查询)

题干&#xff1a; C国的死对头A国这段时间正在进行军事演习&#xff0c;所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段&#xff0c;所以每个工兵营地…

php中获取本月第二天,php第二天

//heredoc方式,可以保存长文本 <<<$str <<长文本EOTS;header("Content-Type:text/html;charsetutf-8"); //防止乱码0 1 2 3 4 5 十进制0 1 10 11 100 101 二进制十进制专二进制除二取余二进制专十进制1011*2*20*2*1(1*2/2)5;120 0 1 0 16 3 211111$a…

【HDU - 1754】I Hate It (线段树模板 单点覆盖更新+区间最大值查询)

题干&#xff1a; 很多学校流行一种比较的习惯。老师们很喜欢询问&#xff0c;从某某到某某当中&#xff0c;分数最高的是多少。 这让很多学生很反感。 不管你喜不喜欢&#xff0c;现在需要你做的是&#xff0c;就是按照老师的要求&#xff0c;写一个程序&#xff0c;模拟老…

php微信上传头像,微信小程序怎么上传头像

这次给大家带来的是微信小程序 上传头像的实例详解&#xff0c;最近在做微信小程序上传头像和上传照片功能就随手写一下代码&#xff0c;这篇文章就给大家好好分析一下。上传头像html&#xff1a;头像JS代码// 切换头像changeAvatar: function () {var that this;// var child…