UVAL 7902 2016ECfinal F - Mr. Panda and Fantastic Beasts
题意:
给出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的更多相关文章
- 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 ...
- [acm/icpc2016ChinaFinal][CodeforcesGym101194] Mr. Panda and Fantastic Beasts
地址:http://codeforces.com/gym/101194 题目:略 思路: 这题做法挺多的,可以sam也可以后缀数组,我用sam做的. 1.我自己yy的思路(瞎bb的) 把第一个串建立s ...
- Gym 101194F Mr. Panda and Fantastic Beasts
#include<bits/stdc++.h> using namespace std; #define ms(arr,a) memset(arr,a,sizeof arr) #defin ...
- hdu6007 Mr. Panda and Crystal 最短路+完全背包
/** 题目:hdu6007 Mr. Panda and Crystal 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6007 题意:魔法师有m能量,有n ...
- 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-中国剩余定理+同余定 ...
- 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 ...
- Gym101194J Mr.Panda and TubeMaster 二分图、费用流
传送门 看到这张图,是一个网格图,而且有回路限制,不难想到黑白染色. 一般来说我们对一张图黑白染色之后都是黑色点向白色点连边,但是这道题往这边想似乎就想不出建图方法了,因为"一个格子强制流满 ...
- (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解密,首先 ...
随机推荐
- “Hello World!”团队第六周第六次会议
“Hello World!”团队第六周第六次会议 博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.checkout& ...
- "私人助手"NABCD分析
---恢复内容开始--- 团队开发项目“私人助手”需求分析NABCD模型: (1)N(Need需求):“私人助手”解决了几类人遇到非常多的事情,非常繁琐,“私人助手”为用户解决这个问题,让用户的工作更 ...
- 四则运算《《《《SQL出题
设计思路: 这次要用数据库存储题目,我想到的是用SQL server数据库,用dataGridView控件读取数据. 具体实现: DBCon.cs 1 using System; 2 using Sy ...
- 由RS-232串口到PROFIBUS-DP总线的转换接口设计
转自:http://gongkong.ofweek.com/2013-08/ART-310007-11001-28716256_2.html 1.PROFIBUS-DP网络协议 PROFIBUS的网络 ...
- 转载---Atom编辑器常用快捷键
常用快捷键–亲测及翻译 英文 中文 快捷键 功能 New Window 新建界面窗口 Ctrl + Shift + N 如中文意思 New File 新建文件 Ctrl + N 如中文意思 Open ...
- LeetCode题解:(221) Maximal Square
题目说明 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...
- wpf后台设置颜色(背景色,前景色)
有时候你是不是也会遇到要在wpf后台给某个控件设置背景色或者给文字设置前景色的情况? 本人最近看到一个从自定义的combobox读取系统字体和颜色的实例,该实例实现了随字体combobox选项改变而改 ...
- python基础(五)函数
一.函数概念 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过程或子程序),在Pascal ...
- Robot Framework 教程 (7) - 使用For循环
在自动化测试过程中,使用For循环来对某个动作进行重复操作是很普遍的行为.在Robot Framework中,各种测试库中均提供了多种方式的For循环结构,在其中覆盖了大部分类型的循环类型.而Robo ...
- (暂时弃坑)(半成品)ACM数论之旅18---反演定理 第二回 Mobius反演(莫比乌斯反演)((づ ̄3 ̄)づ天才第一步,雀。。。。)
莫比乌斯反演也是反演定理的一种 既然我们已经学了二项式反演定理 那莫比乌斯反演定理与二项式反演定理一样,不求甚解,只求会用 莫比乌斯反演长下面这个样子(=・ω・=) d|n,表示n能够整除d,也就是d ...