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 ...
随机推荐
- 新建jsp文件,The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误解决方法
新建一个jsp文件后,有一个错误,The superclass "javax.servlet.http.HttpServlet" was not found on the Java ...
- Elasticsearch 核心术语概念
Elasticsearch 相当于一个关系型数据库 索引 index 类型 type 文档 document 字段 fields 跟关系型数据库对比 Elasticsearch 相当于一个数据库 索引 ...
- 浅谈Java构造器
Java构造器 每个类都有构造方法.如果没有显式地为类定义构造方法,Java编译器将会为该类提供一个默认构造方法. 在创建一个对象的时候,至少要调用一个构造方法.构造方法的名称必须与类同名,一个类可以 ...
- docker-compose错误
1.错误信息: ERROR: for gamehall Get https://hub.tondeen.com/v1/_ping: http: server gave HTTP response to ...
- MySQL入门,第七部分,单表查询
首先我们需要了解一下整个数据库的结构 其中Student表中Sno为主键.Study表中Sno和Cno合起来做主键.Course表中Cno为主键 其创建脚本如下: #----------------- ...
- MySQL InnoDB存储引擎体系架构 —— 索引高级
转载地址:https://mp.weixin.qq.com/s/HNnzAgUtBoDhhJpsA0fjKQ 世界上只两件东西能震撼人们的心灵:一件是我们心中崇高的道德标准:另一件是我们头顶上灿烂的星 ...
- git log查看某文件的修改历史
1. git log filename 可以看到fileName相关的commit记录 2. git log -p filename可以显示每次提交的diff 3. 只看某次提交中的某个文件变化,可以 ...
- 28.4 Calendar 日历
/* * Calendar:日历,提供了一些操作年月日时的方法 * 获取 * 修改 * 添加 */ public class CalendarDemo { public static void mai ...
- python常用算数运算符、比较运算符、位运算符与逻辑运算符
编辑时间: 2019-09-04,22:58:49 算数运算符 '+'.'-'.'*'.'/' :加.减.乘.除 '**':指数运算, ‘//’:整除, ‘%‘:求余数 num_1 = 15; num ...
- 内存对齐 align
/* 地址对齐:指的是存放数据的首地址%N==0,而且整个结构体的大小%M(结构体的有效对齐值)==0 1 数据类型的自身对齐值:char:1 short:2 int,flolat,double:4 ...