【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; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
随机推荐
- 如何的退出无响应的 SSH 连接
~. 具体操作是Shift+-,然后松开按.. tips如果无效,可以先按下Enter,然后进行上面的操作.
- P1491 集合位置 次短路
这个题是一个次短路的裸题,就是把最短路路径求出来之后依次删边,然后跑最短路,在这些情况里取最小值就行了. 题干: 每次有大的活动,大家都要在一起“聚一聚”,不管是去好乐迪,还是避风塘,或者汤姆熊,大家 ...
- bzoj2253
cdq分治+dp 看见三维偏序是cdq,互相包含是最长上升子序列 这个代码是错的 交了两份代码,发现手动出数据是不一样的... 不调了 #include<bits/stdc++.h> us ...
- [转]完整教程--idea使用git进行项目管理
完整教程--idea使用git进行项目管理 第一部分: 安装 1. 下载地址: https://git-scm.com/download/win; 如果速度慢, 使用 迅雷下载; 2. 点击安装, ...
- 删除".SVN"文件夹方法(转载)
转自:http://www.cnblogs.com/lr-ting/archive/2012/09/03/2666271.html 一.在linux下 删除这些目录是很简单的,命令如下 find . ...
- Redis学习和应用记录(1)--介绍和安装
Redis是一个开源的分布式缓存框架,它也常被理解为数据结构服务器,因为它包含丰富的数据类型,如strings, hashes, lists, sets, sorted sets, bitmaps a ...
- PCB SQL Server 触发器应用实例
这里以实际例子对触发器的应用对触发器的理解与应用来得更实际 一.更新触发器(Update) 临时表:inserted表有数据(新数据) Deleted表有数据(旧数据) 实例说明:当表更新时, ...
- null, undefined,"",0,false是什么关系?
null本质上和0,"",false是一类东西,它们都表示一种数据类型的非值.正如0表示数字类型的非值,""表示字符类型的非值一样,null表示完全空的对象,即 ...
- Testng1
Testng 简介: Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性: annotations 注释,如 @test ...
- BOM 标记
BOM 是 Byte Order Mark 的简称,即字节序标记.用于标记文本流: 表示文本流的字节顺序,是小端序(little-endian)还是大端序(big-endian); 表示文本流是 Un ...