【codeforces 510C】Fox And Names
【题目链接】:http://codeforces.com/contest/510/problem/C
【题意】
给你n个字符串;
问你要怎么修改字典序;
(即原本是a,b,c..z现在你可以修改每个字母在字典序中的位置了);
才能使得这n个字符串是字典序升序的;
【题解】
对于第i个字符串;
让他和第i+1..n个字符串比较->j;
找到第一个不同的位置pos;
然后
必然是要
s[i][pos]< s[j][pos]的;
拓扑排序!
即s[i][pos]建一条边指向s[j][pos]
然后做拓扑排序就好;
如果某个串是另外一个串的子串;
则如果s[i]是s[j]的子串,那么可行,否则无解;
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define pri(x) cout << x
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int MAXC = 26+10;
const int N = 100+10;
int a[MAXC][MAXC],n,du[MAXC],cnt=0;
queue <int> dl;
string s[N];
vector <int> ans;
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false);
rei(n);
rep1(i,1,n)
rei(s[i]);
rep1(i,1,n)
{
rep1(j,i+1,n)
{
int ma = min(int(s[i].size()),int(s[j].size()));
int pos = -1;
rep1(k,0,ma-1)
if (s[i][k]!=s[j][k])
{
pos = k;
break;
}
if (pos==-1)
{
if (int(s[i].size())>int(s[j].size()))
return pri("Impossible"<<endl),0;
else
continue;
}
int x = s[i][pos]-'a'+1,y = s[j][pos]-'a'+1;
if (a[x][y]) continue;
du[y]++;
a[x][y]=1;
}
}
rep1(i,1,26)
if (du[i]==0)
dl.push(i),cnt++,ans.ps(i);
while (!dl.empty())
{
int x = dl.front();
dl.pop();
rep1(j,1,26)
if (a[x][j])
{
a[x][j] = 0;
du[j]--;
if (du[j]==0)
{
dl.push(j);
ans.ps(j);
cnt++;
}
}
}
if (cnt==26)
rep1(i,0,25)
putchar(ans[i]+'a'-1);
else
return pri("Impossible"<<endl),0;
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 510C】Fox And Names的更多相关文章
- 【codeforces 510D】Fox And Jumping
[题目链接]:http://codeforces.com/contest/510/problem/D [题意] 你可以买n种卡片; 每种卡片的花费对应c[i]; 当你拥有了第i种卡片之后; 你可以在任 ...
- 【codeforces 510B】Fox And Two Dots
[题目链接]:http://codeforces.com/contest/510/problem/B [题意] 让你在一个二维的方格里面找环; 两个点有相邻的边它们才是相连的; 有环YES,没环NO ...
- 【codeforces 510A】Fox And Snake
[题目链接]:http://codeforces.com/contest/510/problem/A [题意] 让你画一条蛇.. [题解] 煞笔提 [Number Of WA] 0 [完整代码] #i ...
- (CodeForces 510C) Fox And Names 拓扑排序
题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 791C】Bear and Different Names
[题目链接]:http://codeforces.com/contest/791/problem/C [题意] 给你n-k+1个限制 要求 a[i]..a[i]+k-1里面有相同的元素,或全都不同; ...
- 【38.02%】【codeforces 625B】War of the Corporations
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
随机推荐
- java普通代码块、静态代码块、默认构造方法的执行顺序
package test; class Parent{ { System.out.println("父类普通代码块"); } static{ System.out.println( ...
- 马拉车算法(Manacher's Algorithm)
这是悦乐书的第343次更新,第367篇原创 Manacher's Algorithm,中文名叫马拉车算法,是一位名叫Manacher的人在1975年提出的一种算法,解决的问题是求最长回文子串,神奇之处 ...
- 使用Java生成word文档(附源码)
当我们使用Java生成word文档时,通常首先会想到iText和POI,这是因为我们习惯了使用这两种方法操作Excel,自然而然的也想使用这种生成word文档.但是当我们需要动态生成word时,通常不 ...
- C#学习-图片的处理
1.在图片上加防伪标记 private void btnAddString_Click(object sender, EventArgs e) { //以流的方式,获取一张图片 using (File ...
- Java_注解之二
在上一次的注解案例里面配置注解的同时,也添加了一对多(@OneToMany)的关系在里面. 本次将补充上次的缺失:其他三种关联方式的配置. 为了简化配置的复杂度 在此案例中Emp和Dept并不是唯 ...
- Android基于HttpUrlConnection类的文件下载
/** * get方法的文件下载 * <p> * 特别说明 android中的progressBar是google唯一的做了处理的可以在子线程中更新UI的控件 * * @param pat ...
- 02--SQLite操作一步到位
SQLite数据库(一):基本操作 SQLite 是一个开源的嵌入式关系数据库,实现自包容.零配置.支持事务的SQL数据库引擎. 其特点是高度便携.使用方便.结构紧凑.高效.可靠. 与其他数据库管理系 ...
- JS——switch case
语法: switch(n) { case 1: 执行代码块 1 break; case 2: 执行代码块 2 break; default: n 与 case 1 和 case 2 不同时执行的代码 ...
- Spring学习_day02_AOP,AspectJ,JdbcTemplate
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! Spring_day02 一.AOP面向切面编程 1. ...
- ( 转)Hibernate常用API
http://blog.csdn.net/yerenyuan_pku/article/details/65103203 可在度娘上摘抄如下文字: Hibernate的核心类和接口一共有6个,分别为:S ...