Codeforces Round #290 (Div. 2) 拓扑排序
2 seconds
256 megabytes
standard input
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 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 tiaccording to their order in alphabet.
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.
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).
3
rivest
shamir
adleman
bcdefghijklmnopqrsatuvwxyz
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Impossible
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
aghjlnopefikdmbcqrstuvwxyz
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
acbdefhijklmnogpqrstuvwxyz 题意:给你n个字符串 现在要求你重新定义字典序 使得n个字符串的顺序为升序 输出新的26个字母的字典序
题解:对相邻的两个字符串 在第一个字符不同的位置的两个字符间连一条单向边 拓扑排序
#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
#define LL long long
#define mod 1000000007
using namespace std;
char a[][];
int g[][];
int in[];
queue<int> q;
int vis[];
int ans[];
int jishu=;
int n;
bool topsort(){
while(!q.empty())
q.pop();
for(int i=;i<=;i++){
if(in[i]==){
q.push(i);
vis[i]=;
}
}
jishu=;
int exm;
while(!q.empty()){
exm=q.front();
ans[jishu++]=exm;
q.pop();
for(int i=;i<=;i++){
if(g[exm][i]==){
if(--in[i]==&&vis[i]==){
vis[i]=;
q.push(i);
}
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%s",a[i]);
for(int i=;i<n-;i++)
{
int l1,l2;
l1=strlen(a[i]);
l2=strlen(a[i+]);
int p=;
while(p<min(l1,l2)&&a[i][p]==a[i+][p]) p++;
if(p==l1&&l1<l2) continue;
if(p==l2&&l2<l1) {
printf("Impossible\n");//**
return ;
}
if(g[a[i][p]-'a'][a[i+][p]-'a']) continue;
g[a[i][p]-'a'][a[i+][p]-'a']=;
in[a[i+][p]-'a']++;
}
topsort();
if(jishu==){
for(int i=;i<=;i++)
printf("%c",ans[i]+'a');
}
else
printf("Impossible\n");
return ;
}
Codeforces Round #290 (Div. 2) 拓扑排序的更多相关文章
- 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names
题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...
- CodeForces Round #290 Div.2
A. Fox And Snake 代码可能有点挫,但能够快速A掉就够了. #include <cstdio> int main() { //freopen("in.txt&quo ...
- Codeforces Round #290 (Div. 2) C. Fox And Names dfs
C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...
- Codeforces Round #290 (Div. 2) D. Fox And Jumping dp
D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs
B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...
- Codeforces Round #290 (Div. 2) A. Fox And Snake 水题
A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...
- Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模
E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)
http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...
- Codeforces Round #290 (Div. 2) B (dfs)
题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相 ...
随机推荐
- yarn资源memory与core计算配置
yarn调度分配主要是针对Memory与CPU进行管理分配,并将其组合抽象成container来管理计算使用 memory配置 计算每台机子最多可以拥有多少个container: container ...
- CocoaPods did not set the base configuration of your project because your project already has a custom config set.
今天在封装自己的消息推送SDK的时候,pod install 的时候,突然报这个错误,解决方式如下: $ pod install Analyzing dependencies Downloading ...
- 通过exp命令对Oracle数据库进行备份操作(提供两种情况的备份:备份本地,备份远程的数据库)
exp 用户名/密码@数据库所在ip地址:数据库端口号/数据库的service-name file=存储到的位置 这个是能成功的 http://www.2cto.com/database/201402 ...
- 北航MOOC系统Android客户端NABC
北航MOOC手机客户端NABC分析 1) N (Need 需求) MOOC是Massive Open Online Course的缩写,通常被译为大型开放式网络课程,它最早在08年的时候由一位加拿大的 ...
- [2019BUAA软件工程]第0次个人作业
我 & 计算机 写在前面 撰写本博客时,笔者正就读北航计算机系大三下的软件工程课程.借由这次博客作业的机会,笔者从高考时与计算机专业结缘.大学对计算机的学习以及对未来的计划三方面进行了些许 ...
- 20172329 2018-2019 《Java软件结构与数据结构》实验三报告
20172329 2018-2019-2 <Java软件结构与数据结构>实验三报告 课程:<Java软件结构与数据结构> 班级: 1723 姓名: 王文彬 学号:2017232 ...
- 20162319 实验四 Android程序设计
Android Stuidio的安装测试: 完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号 ·实验过程 完成任务一,只需在Android应用程序文件 ...
- 读《构建之法》一、二、十六章随笔a
第一章 概论 “软件团队要从需求分析开始,把合适的需求梳理出来,然后逐步开展后续工作”:——p3 问题:好的用户体验要从软件分析开始,那么软件分析仅仅是从用户的需求出发吗? 我的看法:需求分析是 ...
- 由一个滑动条的任务需求产生一个对UISlider控件的探讨
任务需求样式:
- cxGrid 单元格回车移到下一行,当移到最后一个单元格时回车新增一行【转】
1 在TcxGridDBTableView中,设定属性 NewItemRow.Visible = True 2 在cxgrid中输入数据怎样回车换行 在TcxGridDBTableView中 将属 ...