题意:

给出n个串,求一个最短的第一个串的子串使它不在其他的n-1个串中出现,若有多个求字典序最小的。

Limits: • 1 ≤ T ≤ 42. • 2 ≤ N ≤ 50000. • N ≤ S1 + S2 + · · · + SN ≤ 250000. • the sum of Si in all test cases doesn’t exceed 3 × 106 .

Sample Input

3

2

aba

bab

3

qnu

cvbb

bnu

3

a

aa

aaa

Sample Output

Case #1: aba

Case #2: q

Case #3: Impossible

代码:

//这么多串肯定要把他们连接起来(之间用没出现的字符隔开)。后缀数组求出heigh数组,答案要求字典序最小所以后缀数组从前向后找即可,每找到
//一个在第一个串中的i位置时,在该位置向前和向后各找到第一个不在第一个串中的j那么lcp(i,j)一定是i位置和其他不在第一个串中后缀的
//最长的lcp,那么这个lcp+1的长度就一定是在其他串中没有出现过,要求字典序最小所以要向后扩展一位,并且这个长度不能超出第一个串。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
int t,n,sa[MAXN],he[MAXN],ra[MAXN],xx[MAXN],yy[MAXN],buc[MAXN];
char s[MAXN],ch[MAXN];
int len,len1,m;
void get_suf()
{
int *x=xx,*y=yy;
for(int i=;i<m;i++) buc[i]=;
for(int i=;i<len;i++) buc[x[i]=s[i]]++;
for(int i=;i<m;i++) buc[i]+=buc[i-];
for(int i=len-;i>=;i--) sa[--buc[x[i]]]=i;
for(int k=;k<=len;k<<=){
int p=;
for(int i=len-;i>=len-k;i--) y[p++]=i;
for(int i=;i<len;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
for(int i=;i<m;i++) buc[i]=;
for(int i=;i<len;i++) buc[x[y[i]]]++;
for(int i=;i<m;i++) buc[i]+=buc[i-];
for(int i=len-;i>=;i--) sa[--buc[x[y[i]]]]=y[i];
swap(x,y);
p=;x[sa[]]=;
for(int i=;i<len;i++){
if(y[sa[i-]]==y[sa[i]]&&y[sa[i-]+k]==y[sa[i]+k])
x[sa[i]]=p-;
else x[sa[i]]=p++;
}
if(p>=len) break;
m=p;
}
for(int i=;i<len;i++) ra[sa[i]]=i;
int k=;
for(int i=;i<len;i++){
if(ra[i]==) { he[]=;continue; }
if(k) k--;
int j=sa[ra[i]-];
while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++;
he[ra[i]]=k;
}
}
void solve()
{
int ans=len,pos=-;
for(int i=;i<len;i++){
while(i<len&&sa[i]>=len1) i++;
if(i>=len) break;
int plcp=he[i];
for(int j=i-;j>=;j--){
if(sa[j]>=len1) break;
plcp=min(plcp,he[j]);
}
int slcp=he[i+];
for(int j=i+;j<=len;j++){
if(sa[j]>=len1) break;
slcp=min(slcp,he[j]);
}
plcp=max(plcp,slcp);
if(plcp<len1-sa[i]){
if(plcp+<ans){
ans=plcp+;
pos=sa[i];
}
}
} if(pos==-) puts("Impossible");
else{
for(int i=;i<ans;i++)
printf("%c",s[i+pos]);
puts("");
}
}
int main()
{
scanf("%d",&t);
for(int cas=;cas<=t;cas++){
scanf("%d",&n);
scanf("%s",s);
len=len1=strlen(s);
for(int i=;i<=n;i++){
s[len++]='#';
scanf("%s",s+len);
len=strlen(s);
}
m=;
get_suf();
printf("Case #%d: ",cas);
solve();
}
return ;
}

UVAL 7902 2016ECfinal F - Mr. Panda and Fantastic Beasts的更多相关文章

  1. 2016 ACM-ICPC China Finals #F Mr. Panda and Fantastic Beasts

    题目链接$\newcommand{\LCP}{\mathrm{LCP}}\newcommand{\suf}{\mathrm{suf}}$ 题意 给定 $n$ 个字符串 $s_1, s_2, \dots ...

  2. 2016EC Final F.Mr. Panda and Fantastic Beasts

    题目大意 \(T(1\leq T\leq42)\)组数据,给定\(n(2\leq n\leq 50000)\)个字符串\(S_{i}(n\leq\sum_{i=1}^{n}S_{i}\leq 2500 ...

  3. [acm/icpc2016ChinaFinal][CodeforcesGym101194] Mr. Panda and Fantastic Beasts

    地址:http://codeforces.com/gym/101194 题目:略 思路: 这题做法挺多的,可以sam也可以后缀数组,我用sam做的. 1.我自己yy的思路(瞎bb的) 把第一个串建立s ...

  4. Gym 101194F Mr. Panda and Fantastic Beasts

    #include<bits/stdc++.h> using namespace std; #define ms(arr,a) memset(arr,a,sizeof arr) #defin ...

  5. hdu6007 Mr. Panda and Crystal 最短路+完全背包

    /** 题目:hdu6007 Mr. Panda and Crystal 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6007 题意:魔法师有m能量,有n ...

  6. 2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定理

    2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定 ...

  7. H - Mr. Panda and Birthday Song Gym - 101775H (动态规划)

    Mrs. Panda’s birthday is coming. Mr. Panda wants to compose a song as gift for her birthday. It is k ...

  8. Gym101194J Mr.Panda and TubeMaster 二分图、费用流

    传送门 看到这张图,是一个网格图,而且有回路限制,不难想到黑白染色. 一般来说我们对一张图黑白染色之后都是黑色点向白色点连边,但是这道题往这边想似乎就想不出建图方法了,因为"一个格子强制流满 ...

  9. (CCPC-Final 2018)K - Mr. Panda and Kakin

    题意:x是\([1e5,1e9]\)的随机数,p是小于x的最大素数,q是大于等于x的最小素数,\(n=pq\),\(c=f^{2^{30}+3}\mod{n}\),给n和c求f 题解:rsa解密,首先 ...

随机推荐

  1. 3.控制hive map reduce个数

    参考: https://blog.csdn.net/wuliusir/article/details/45010129 https://blog.csdn.net/zhong_han_jun/arti ...

  2. OGG 跳过事务(转)

    http://blog.chinaunix.net/uid-26190993-id-3434074.html    在OGG运行过程中,通常会因为各种各样的原因导致容灾端的REPLICAT进程ABEN ...

  3. 第10讲:利用SQL语言实现关系代数操作

    一.实现并.交.差运算 1. 基本语法形式:子查询 [union [all] | intersect [all] | except [all] 子查询] ①意义:将关系代数中的∪.∩.- 分别用uni ...

  4. 2018软工实践第八次作业-团队项目UML设计

    团队信息 队员姓名与学号 学号 姓名 博客链接 124 王彬(组长) 点击这里 206 赵畅 点击这里 215 胡展瑞 点击这里 320 李恒达 点击这里 131 佘岳昕 点击这里 431 王源 点击 ...

  5. ADO.NET使用using关闭数据库连接

    using (SqlConnection conn = new SqlConnection(source)) { // open the connoction conn.Open(); // Do s ...

  6. grunt入门讲解4:如何创建task(任务)

    每当运行Grunt时, 你可以为其指定一个或多个任务, 这些任务用于告诉Grunt你想要它做什么事情. 如果你没有指定一个任务,并且你已经定义一个名为 "default" 的任务, ...

  7. 预则立&&他山之石--团队计划、访谈优秀前辈

    团队计划&访谈内容 一.团队计划 序号 任务内容 计划完成时间 主要负责人 备注 1 对接教师报课系统 决定是否重构代码 2016.10.16 陈少铭.黄家俊 阅读CourseManageme ...

  8. TP5 多入口文件配置的坑

    闲话不多说,TP5(5.0.20) 在配置多入口文件的时候你是否遇到过一下的问题呢? 开发设计的需求吧网站拆分为前台.后台.API 3 个模块,对应的也需要3个入口文件,后台和API入口文件是用PAT ...

  9. 使用Ubuntu编译Linux内核

    1.下载内核并解压到 /usr/src 目录下 在终端执行以下命令即可下载 4.16.14版本(目前最新的稳定版)的内核到当前shell打开的目录下 wget https://cdn.kernel.o ...

  10. CSS 报错