Gym 101194F Mr. Panda and Fantastic Beasts
#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的更多相关文章
- [acm/icpc2016ChinaFinal][CodeforcesGym101194] Mr. Panda and Fantastic Beasts
地址:http://codeforces.com/gym/101194 题目:略 思路: 这题做法挺多的,可以sam也可以后缀数组,我用sam做的. 1.我自己yy的思路(瞎bb的) 把第一个串建立s ...
- UVAL 7902 2016ECfinal F - Mr. Panda and Fantastic Beasts
题意: 给出n个串,求一个最短的第一个串的子串使它不在其他的n-1个串中出现,若有多个求字典序最小的. Limits: • 1 ≤ T ≤ 42. • 2 ≤ N ≤ 50000. • N ≤ S1 ...
- 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 ...
- 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 ...
- 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}$, 并且能被 ...
- Codeforces Gym 101194C Mr. Panda and Strips(2016 EC-Final,区间DP预处理 + 枚举剪枝)
题目链接 2016 EC-Final 题意 现在要找到数列中连续两个子序列(没有公共部分).要求这两个子序列本身内部没有重复出现的数. 求这两个子序列的长度的和的最大值. 首先预处理一下.令$ ...
- Gym - 101194F(后缀数组)
Mr. Panda and Fantastic Beasts 题意 给出若干个字符串,找到一个最短的字典序最小的字符串且仅是第一个字符串的子串. 分析 对于这种多个字符串.重复的子串问题一般都要连接字 ...
- 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 ...
- hdu6007 Mr. Panda and Crystal 最短路+完全背包
/** 题目:hdu6007 Mr. Panda and Crystal 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6007 题意:魔法师有m能量,有n ...
随机推荐
- 拿万元月薪必备的书单,学JAVA的程序员必看的5本书!
点击蓝色"程序员黄小斜"关注我哟 加个"星标",每天带你读好书! 文/黄小斜 转载请注明出处 每一年的年初都是买书学习热情高涨的时候,虽然不知道你们是让这些书吃 ...
- flex布局取消子元素(img、div等)缩放:
取消子元素(img.div等)缩放: 父元素: display: flex ; 子元素: flex-shrink: 0;
- IIS 组成
HTTP.sys http.sys 侦听来自网络的 HTTP 请求,将它们传递到 IIS 并返回响应. 它是一种可以从命令行停止和启动的服务. "NET STOP HTT ...
- uni-app在线引入阿里字体图标库
第一步 在app.vue中引入阿里字体图标库 第二步 在任意页面使用就可以了 <view class="item" v-for="(value,index) in ...
- Python Modules and Packages – An Introduction
This article explores Python modules and Python packages, two mechanisms that facilitate modular pro ...
- Android 圆形图片库 CircleImageView
高仿微信朋友圈 10s 视频裁剪 引语 晚上好,我是猫咪,我的公众号「程序媛猫咪」会推荐 GitHub 上好玩的项目,挖掘开源的价值,欢迎关注我. <Android 图片裁剪库 uCrop> ...
- Python爬虫利器 cURL你用过吗?
hello,小伙伴们,今天给大家分享的开源项目是一个python爬虫利器,感兴趣的小伙伴看完这篇文章不妨去尝试一下,这个开源项目就是curlconverter,不知道小伙伴们分析完整个网站后去code ...
- 中阶d03.1 JDBCDemo
1. jdbc使用查看驱动的doc文档<connector-j.html> 2.代码实现:1. 注册驱动---2. 建立连接---3. 创建statement ,跟数据库打交道--- -- ...
- "字母全变小写"组件:<lowercase> —— 快应用组件库H-UI
 <import name="lowercase" src="../Common/ui/h-ui/text/c_text_lowercase">& ...
- "五号标题"组件:<h5> —— 快应用组件库H-UI
 <import name="h5" src="../Common/ui/h-ui/text/c_h5"></import> < ...