#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. 2783: 【基础】小 X 玩游戏(game)

    2783: [基础]小 X 玩游戏(game) 时间限制: 1 Sec 内存限制: 64 MB 提交: 752 解决: 294 [提交] [状态] [讨论版] [命题人:ghost79] 题目描述 听 ...

  2. webpack-bundle-analyzer打包文件分析工具

    一.安装 npm intall webpack-bundle-analyzer –save-dev 二.配置 在build/webpack.prod.config.js中的module.exports ...

  3. 1006 Sign In and Sign Out (25 分)

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  4. 本地Vue项目跨域请求本地Node.js服务器的配置方法

    前言:跨域请求是在本地开发时经常遇到的需求,也很简单,只是几句代码配置一下的问题.我初次配置跨域请求时由于官方的说明太简洁,找到的教程又落伍,调试了一番并没有解决问题,到最后解决问题,已花费了很多时间 ...

  5. 部署mysql主从同步

                                                 mysql-day06        部署mysql主从同步 案例拓扑 • 一主.一从 – 单向复制时,建议将 ...

  6. WIFI:802.11无线LAN

    IEEE 802.11 无线LAN(也称WiFi) IEEE是什么 电气和电子工程师协会(IEEE,全称是Institute of Electrical and Electronics Enginee ...

  7. 关于json语句的相关用法

    json语句: JSON 值可以是: 数字(整数或浮点数)字符串(在双引号中)逻辑值(true 或 false)数组(在中括号中)对象(在大括号中)null 对于json的的对象数组:var site ...

  8. Scrapy-02-item管道、shell、选择器

    Scrapy-02 item管道: scrapy提供了item对象来对爬取的数据进行保存,它的使用方法和字典类似,不过,相比字典,item多了额外的保护机制,可以避免拼写错误和定义字段错误. 创建的i ...

  9. Java中String转int型的方法以及错误处理

    应要求,本周制作了一个判断一个年份是否是闰年的程序.逻辑很简单,这里就不贴代码了.可是,在这次程序编写中发现了一个问题. 在输入年份时,如果输入1)字母2)空3)超过Int上限时,就会抛excepti ...

  10. PHP中嵌入正则表达式常用的函数

    PHP中嵌入正则表达式常用的函数有四个: 1.preg_match() :preg_match() 函数用于进行正则表达式匹配,成功返回 1 ,否则返回 0 . 语法:int preg_match( ...