【题目链接】: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. [Codeforces 485F] Oppa Funcan Style Remastered

    [题目链接] https://codeforces.com/contest/986/problem/F [算法] 不难发现 , 每个人都在且仅在一个简单环中 , 设这些环长的长度分别为 A1, A2 ...

  2. Luogu3674小清新人渣的本愿

    https://zybuluo.com/ysner/note/1109536 题面 给你一个序列a,长度为n,有m次操作,每次询问一个区间 是否可以选出两个数它们的差为x 是否可以选出两个数它们的和为 ...

  3. hadoop hbase 快速启动 关闭 配置:

    vim ~/.bashrcexport JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64   //JDK安装路径export HADOOP_HOME=/usr/l ...

  4. thinkphp调试手段

    使用ThinkPHP应该掌握的调试手段经常看到有人问到findAll的返回数据类型是什么之类的问题,以及出错了不知道什么原因的情况,其实还是没有熟悉ThinkPHP内置的调试手段和方法,抛开IDE本身 ...

  5. [Swift通天遁地]五、高级扩展-(9)颜色、设备、UserDefaults、URL等扩展方法

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. day-06 文件读写

    # # -*- coding: utf-8 -*-''' r 读 w 写 a 追加写 r+ 读写 w+ 读写 a+读写 rb 读字节 wb 写字节 w r 是文本 rb wb非文本 ../ (相对路径 ...

  7. 局部覆盖element-ui的默认样式

    最近项目中遇到的问题,只想在某个页面里面单独更改element-ui的样式,而不影响全局 有两种方法: 1.在需要更改的组件里新增一个style标签[重点:不要加scoped],然后直接获取class ...

  8. SS配置,Brook是什么?,Brook如何配置(Android篇)

    很长时间没有更新了,今天给大家分享一下什么是Brook,和SS有什么区别?写的不好,请勿见外,大佬绕过. Brook简单介绍 Brook 是一个高效的 Socks5 代理软件,官方支持Windows. ...

  9. centos源更新

    .备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup .下载新的CentOS-Base.r ...

  10. 执行update, insert,delete 语句, 不返回结果集,(类型化参数)

    /// <summary> /// 执行update, insert,delete 语句, 不返回结果集,(类型化参数) /// </summary> /// <para ...