#include<bits/stdc++.h>
using namespace std;
#define ms(arr,a) memset(arr,a,sizeof arr)
#define debug(x) cout<<"< "#x" = "<<x<<" >"<<endl
const int maxn=4e5;
const int INF=0x3f3f3f3f;
char s[maxn];
int sa[maxn],Rank[maxn],height[maxn],a[maxn],b[maxn],rmq[maxn][50];
int x[maxn],y[maxn],c[maxn];
int len,tot;
void build()
{
int n=strlen(s),m=128;
//int *x=new int[n],*y=new int[n],*c=new int[n+128];
for(int i=0;i<m;++i)c[i]=0;
for(int i=0;i<n;++i)c[x[i]=s[i]]++;
for(int i=1;i<m;++i)c[i]=c[i]+c[i-1];
for(int i=n-1;i>=0;--i)sa[--c[x[i]]]=i;
for(int k=1;k<=n;k<<=1)
{
int p=0;
for(int i=n-k;i<n;++i)y[p++]=i;
for(int i=0;i<n;++i)if(sa[i]>=k)y[p++]=sa[i]-k;
for(int i=0;i<m;++i)c[i]=0;
for(int i=0;i<n;++i)c[x[y[i]]]++;
for(int i=1;i<m;++i)c[i]+=c[i-1];
for(int i=n-1;i>=0;--i)sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=1;x[sa[0]]=0;
for(int i=1;i<n;++i)x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
if(p>=n)break;
m=p;
}
//delete[] x,y,c;
for(int i=0;i<n;++i)Rank[sa[i]]=i;
int len=0;
for(int i=0;i<n;++i)
{
if(len)len--;
if(Rank[i]==0)continue;
int j=sa[Rank[i]-1];
while(s[i+len]==s[j+len])len++;
height[Rank[i]]=len;
}
for(int i=1;i<n;++i)rmq[i][0]=height[i];
for(int i=1;(1<<i)<n;++i)for(int j=1;j+(1<<i)<=n;++j)rmq[j][i]=min(rmq[j][i-1],rmq[j+(1<<(i-1))][i-1]);
}
int lcp(int a,int b)
{
if(a==b)return strlen(s+sa[a]);
if(a>b)swap(a,b);
int k=0;
while((1<<(k+1))<=b-a)k++;
return min(rmq[a+1][k],rmq[b-(1<<k)+1][k]);
}
int main()
{
freopen("Input.txt","r",stdin);
int T;scanf("%d",&T);
for(int Case=1;Case<=T;++Case)
{ int n;
scanf("%d%s",&n,s);
len=strlen(s);
for(int i=0;i<n-1;++i)
{
int pos=strlen(s);
s[pos]='.';
scanf("%s",s+pos+1);
}
tot=strlen(s);
build();
int tmp=0;
a[0]=-1,b[tot-1]=-1;
for(int i=1;i<tot;++i)
{
if(sa[i-1]<len)a[i]=a[i-1];
else a[i]=i-1;
if(sa[tot-i]<len)b[tot-i-1]=b[tot-i];
else b[tot-i-1]=tot-i;
}
int ans=INF,ansp;
for(int i=0;i<len;++i)
{
int pos=Rank[i],mLen;
if(a[pos]==-1)mLen=lcp(pos,b[pos]);
else if(b[pos]==-1)mLen=lcp(pos,a[pos]);
else mLen=max(lcp(pos,b[pos]),lcp(pos,a[pos]));
if(mLen+i>=len)break;
mLen++;
if(mLen<ans)ans=mLen,ansp=pos;
if(mLen==ans&&pos<ansp)ansp=pos;
}
printf("Case #%d: ",Case);
if(ans==INF)printf("Impossible\n");
else
{
for(int i=0;i<ans;++i)printf("%c",s[sa[ansp]+i]);
printf("\n");
}
}
}

Gym 101194F Mr. Panda and Fantastic Beasts的更多相关文章

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

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

  2. UVAL 7902 2016ECfinal F - Mr. Panda and Fantastic Beasts

    题意: 给出n个串,求一个最短的第一个串的子串使它不在其他的n-1个串中出现,若有多个求字典序最小的. Limits: • 1 ≤ T ≤ 42. • 2 ≤ N ≤ 50000. • N ≤ S1 ...

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

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

  5. Codeforces Gym 101775D Mr. Panda and Geometric Sequence(2017-2018 ACM-ICPC Asia East Continent League Final,D题,枚举剪枝)

    题目链接  ECL-Final 2017 Problem D 题意  给定$2*10^{5}$组询问,每个询问求$l$到$r$之间有多少个符合条件的数 如果一个数小于等于$10^{15}$, 并且能被 ...

  6. Codeforces Gym 101194C Mr. Panda and Strips(2016 EC-Final,区间DP预处理 + 枚举剪枝)

    题目链接  2016 EC-Final 题意  现在要找到数列中连续两个子序列(没有公共部分).要求这两个子序列本身内部没有重复出现的数.   求这两个子序列的长度的和的最大值. 首先预处理一下.令$ ...

  7. Gym - 101194F(后缀数组)

    Mr. Panda and Fantastic Beasts 题意 给出若干个字符串,找到一个最短的字典序最小的字符串且仅是第一个字符串的子串. 分析 对于这种多个字符串.重复的子串问题一般都要连接字 ...

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

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

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

随机推荐

  1. STM32F103C8T6最小系统开发板原理图

    1.

  2. QT-day1 创建项目

  3. 最近准备研读thinkphp源码,ctag派上用场了

    本人习惯用vim编辑器,这里ctags配置vim很方便. #在ThinkPHP源码目录(假定为/server/thinkphp)执行: $ cd /server/thinkphp $ ctags -R ...

  4. 微信小程序mpvue-动态改变navigationBarTitleText值

    通过JS动态 改变navigationBarTitleText的值 能否通过JS动态改变navigationBarTitleText的值? 方法一:可以在onLoad方法中通过wx.setNaviga ...

  5. Vulnhub DC-1靶机渗透

    简介 DC-1靶机是为了入门渗透测试的人准备的入门级的靶机,该靶机共有5个flag,其中最后一个finalflag是真的flag,其他都是提示性flag.这个靶机虽然简单,但是还是能学到一些基本的思路 ...

  6. 听说这个 IP 和子网掩码异常难算

    IP地址格式 每个Internet主机或路由器都有IP地址.所有的IP地址包括网络号和主机号(就像是手机号,前几位是区号,后几位是序列号). 说明如下 A类地址用于主机数目非常多的网络.A类地址允许有 ...

  7. 加密采矿僵尸网路病毒还在蔓延! kinsing kdevtmpfsi redis yarn docker

    Hadoop yarn 加密采矿僵尸网路病毒还在继续蔓延! 解决步骤 如果你同样遇到了kdevtmpfsi异常进程,占用了非常高的CPU和出网带宽,影响到了你的正常业务,建议使用以下步骤解决 杀掉异常 ...

  8. Java第十八天,可变参数

    可变参数 1.使用前提 当一个方法的参数需要多个参数,并且这些参数的类型一致时,可以使用可变参数. 2.使用方法 定义方法时使用 3.定义格式 修饰符 返回值类型 方法名(参数类型...变量名){ } ...

  9. Maven版本不合适导致出现的问题如下,换个老版本就好了

    2019-09-30 11:56:24,555 [ 597097] ERROR - #org.jetbrains.idea.maven - IntelliJ IDEA 2018.3.5 Build # ...

  10. L21 Momentum RMSProp等优化方法

    airfoil4755 下载 链接:https://pan.baidu.com/s/1YEtNjJ0_G9eeH6A6vHXhnA 提取码:dwjq 11.6 Momentum 在 Section 1 ...