【题目链接】: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的更多相关文章

  1. 【codeforces 510D】Fox And Jumping

    [题目链接]:http://codeforces.com/contest/510/problem/D [题意] 你可以买n种卡片; 每种卡片的花费对应c[i]; 当你拥有了第i种卡片之后; 你可以在任 ...

  2. 【codeforces 510B】Fox And Two Dots

    [题目链接]:http://codeforces.com/contest/510/problem/B [题意] 让你在一个二维的方格里面找环; 两个点有相邻的边它们才是相连的; 有环YES,没环NO ...

  3. 【codeforces 510A】Fox And Snake

    [题目链接]:http://codeforces.com/contest/510/problem/A [题意] 让你画一条蛇.. [题解] 煞笔提 [Number Of WA] 0 [完整代码] #i ...

  4. (CodeForces 510C) Fox And Names 拓扑排序

    题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...

  5. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  6. 【codeforces 791C】Bear and Different Names

    [题目链接]:http://codeforces.com/contest/791/problem/C [题意] 给你n-k+1个限制 要求 a[i]..a[i]+k-1里面有相同的元素,或全都不同; ...

  7. 【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 ...

  8. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  9. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

随机推荐

  1. system.web section group下的section

    private Configuration _configuration; private ConfigurationSectionGroupCollection sectionGroups; pri ...

  2. min-max容斥小结

    https://www.zybuluo.com/ysner/note/1248287 定义 对于一个集合\(S\), \(\min(S)\)表示其第一个出现的元素(\(or\)最小的元素), \(\m ...

  3. JSP-Runoob:JSP 服务器响应

    ylbtech-JSP-Runoob:JSP 服务器响应 1.返回顶部 1. JSP 服务器响应 Response响应对象主要将JSP容器处理后的结果传回到客户端.可以通过response变量设置HT ...

  4. shell脚本-循环选择语句

    shell脚本-循环选择语句 过程式编程语言: 顺序执行 选择执行 循环执行 注:条件中的变量,可以在执行语句中使用,不用在加上"$". if语句 根据命令的退出状态来执行命令 单 ...

  5. Java注解Annotation的用法 - 自定义Annotation实现

    Java注解又称Java标注,是Java语言5.0版本开始支持加入源代码的特殊语法元数据. Java语言中的类.方法.变量.参数和包等都可以被标注.和Javadoc不同,Java标注可以通过反射获取标 ...

  6. C# 单例3种写法

    public class Singleton { private static Singleton _instance = null; private Singleton(){} public sta ...

  7. 1.1输出“hello,world”

    #include<iostream> using namespace std; int main() { cout<<"Hello, World!"< ...

  8. mina2 笔记

    http://www.iteye.com/topic/1112123 http://dongxuan.iteye.com/blog/901689 http://scholers.iteye.com/b ...

  9. ORA-01075: you are currently logged on

    [root@hear01 ~]# su - oracle[oracle@hear01 ~]$ sqlplus "/as sysdba" SQL*Plus: Release 11.2 ...

  10. 使用Jupter Notebook实现简单的神经网络

    参考:http://python.jobbole.com/82208/ 注:1)# %matplotlib inline 注解可以使Jupyter中显示图片 2)注意包的导入方式 一.使用的Pytho ...