Codeforces 510C (拓扑排序)
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个字符串(全小写),让你按照输入的顺序来个字母表排列,(就是改变字母表的某些字母位置,使得你的输入是按照新的字典序)
如果前一个是后一个的子串,那么前一个一定小于后一个所以可以跳过。反之,如果后一个是前一个的子串,无论字典序怎么改都无法成立就输出"Impossible"。
新的字典序就是按照2个字符串的不同首字母,构建的有向图。
在使用拓扑排序就可以了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define mset(a, b) memset((a), (b), sizeof(a))
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
string s[maxn];
int gap[][];
int c[], topo[], t;
bool dfs(int u)
{
c[u] = -;
for(int v = ; v<; v++) if(gap[u][v]){
if(c[v] < ) return false;
else if(!c[v] && !dfs(v)) return false;
}
c[u] = ; topo[--t] = u;
return true;
}
bool toposort()
{
t = ;
mset(c, );
for(int i=;i<;i++) if(!c[i])
if(!dfs(i)) return false;
return true;
}
int main()
{
int n;
cin >> n;
for(int i=;i<n;i++) cin >> s[i];
for(int i=;i<n-;i++){
int len1 = s[i].size();
int len2 = s[i+].size();
int p = ;
while(p<len1 && p<len2 && s[i][p]==s[i+][p]) p++;
if(p == len1 && len1 < len2) continue;
if(p == len2 && len2 < len1) {cout<<"Impossible"<<endl;return ;}
if(gap[s[i][p]-'a'][s[i+][p] -'a'] == ) continue;
gap[s[i][p]-'a'][s[i+][p]-'a']=;
}
if(toposort()){
for(int i=;i<;i++)
printf("%c", topo[i]+'a');
}
else
cout <<"Impossible"<< endl;
return ;
}
Codeforces 510C (拓扑排序)的更多相关文章
- CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)
ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...
- CodeForces - 721C 拓扑排序+dp
题意: n个点m条边的图,起点为1,终点为n,每一条单向边输入格式为: a,b,c //从a点到b点耗时为c 题目问你最多从起点1到终点n能经过多少个不同的点,且总耗时小于等于t 题解: 这道 ...
- Codeforces 1100E 拓扑排序
题意及思路:https://blog.csdn.net/mitsuha_/article/details/86482347 如果一条边(u, v),v的拓扑序小于u, 那么(u, v)会形成环,要反向 ...
- National Property CodeForces - 875C (拓扑排序)
大意: n个字符串, 每次操作选出一种字符全修改为大写, 求判断能否使n个字符串字典序非降. 建源点s, 汇点t, s与所有必须转大写的连边, 必须不转大写的与t连边. #include <io ...
- Codeforces 1159E 拓扑排序
题意及思路:https://www.cnblogs.com/dd-bond/p/10859864.html 代码: #include <bits/stdc++.h> #define LL ...
- CodeForces 510C Fox And Names (拓扑排序)
<题目链接> 题目大意: 给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同. 解题分析:本题想到 ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序
B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序
C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
随机推荐
- Cordova指令
安装 cordova: npm install -g cordova 创建应用程序 cordova create hello com.example.hello HelloWorld cordov ...
- 基于docker registry镜像安装私服docker hub
采用docker registry镜像安装docker私服,通过https://hub.docker.com/_/registry链接搜索registry镜像 1.输入命令:docker pull r ...
- 所有硬币组合问题——动态规划hdu2069
Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cen ...
- Win7 VSCode 离线安装Rust语言及环境配置
前置依赖 装过Visual Studio或Visual Studio Build Tool 2015 下载Rust离线安装包 https://forge.rust-lang.org/other-ins ...
- jQury+Ajax与C#后台交换数据
-------------------------------------------jQury+Ajax调用后台方法----------------------------------------- ...
- JavaWeb防止用户的重复请求提交
这里实现这个重复提交的防止,是通过在一个FIlter过滤器中生成一个令牌token,保存在Session域中,然后在对这个token加密得到ciphertext(密文),将密文保存在request域中 ...
- 记录XorDDos木马清理步骤
1.检查 查看定时任务文件发现有两个异常定时任务 [root@manage ~]# cat /etc/crontab # * * * * * user-name command to be execu ...
- JS原型链详解
最近面试被问到了就决定好好深入理解原型链 对象 要清楚原型链,首先要弄清楚对象: 普通对象 最普通的对象:有__proto__属性(指向其原型链),没有prototype属性. 原型对象(person ...
- mybatis问题整理
// List<String> findBuildByProject(String prjName); //单参数时使用<if></if>标签判断采用"_ ...
- 解决Maven项目BindingException异常
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.offcn.mybatis.m ...