【题目链接】: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. ubuntu安装phpstorm

    首先要安装jdk $ java -version java version "1.8.0_171" Java(TM) SE Runtime Environment (build 1 ...

  2. P2657 [SCOI2009]windy数 数位dp

    数位dp之前完全没接触过,所以NOIP之前搞一下.数位dp就是一种dp,emm……用来求解区间[L,R]内满足某个性质的数的个数,且这个性质与数的大小无关. 在这道题中,dp[i][j]代表考虑了i位 ...

  3. openStack调试

  4. 如何判断js的变量的数据类型

    文章首发: http://www.cnblogs.com/sprying/p/4349426.html 本文罗列了一般的Js中类型检测的方法,实际上是每个新手在构建Js知识体系时,都要知晓的,而我只是 ...

  5. [Apple开发者帐户帮助]七、注册设备(2)注册多个设备

    如果您有许多测试设备,则可以创建包含设备名称和设备ID的文件,并将整个文件上载到开发人员帐户.您的开发人员帐户支持以下两种文件格式:具有.deviceids文件扩展名和纯文本文件的属性列表文件.您选择 ...

  6. max_allowed_packet设置问题

    最近在运行的项目出现了一个线上事故,有人反映商城的东西下不了单了,到后台看了一下,果然报了一个错 Cause: com.mysql.jdbc.PacketTooBigException: Packet ...

  7. jquery实现图片预加载提高页面加载速度

    使用jquery实现图片预加载提高页面加载速度和用户体 我们在做网站的时候经常会遇到这样的问题:一个页面有大量的图片导致页面加载速度缓慢,经常会出现一个白页用户体验很不好.那么如何解决这个问题 呢?首 ...

  8. 【BZOJ3328】PYXFIB(数学)

    什么都不会的数学蒻菜瑟瑟发抖--Orz橙子(和兔子) 题目: BZOJ3328 分析: 橙子给我安利的数学题--(然后我就看着他因为矩阵乘法多模了一次卡了一天常数qwq表示同情) 先考虑一个子问题:求 ...

  9. 【BZOJ1939】[Croatian2010] Zuma(动态规划)

    题目: BZOJ1939(权限题) 分析: 这题很容易看出是DP,但是状态和转移都不是很好想-- 用\(dp[l][r][c]\)表示在\(l\)前面已经新加了\(c\)个和\(l\)一样的弹子时,使 ...

  10. hdu2026(water~~)

    http://acm.hdu.edu.cn/showproblem.php?pid=2026 #include<iostream> #include<stdio.h> #inc ...