地址:http://codeforces.com/gym/101194

题目:略

思路:

  这题做法挺多的,可以sam也可以后缀数组,我用sam做的。

  1.我自己yy的思路(瞎bb的)

    把第一个串建立sam,然后让其他串在上面跑。

    每走到一个位置p,当前长度为num,就代表这个endpos集合里的长度小于等于num的字符串是被包含了,同时parent树的所有祖先节点也要标记为被包含。

    这一步的具体做法是:用一个mi数组表示到达该节p点时的长度大于mi[p]时,才算未被包含(到达长度指的是从root出发经过某条路径到p的长度),所以其他串在sam上走到每个节点p,长度为num时,记录下mi[p]=max(mi[p],num);

    再跑完所有串后,来更新parent树上的节点。先拓扑排序,然后如果mi[p]>0,mi[fa[p]]=len[p],否则mi[p]=len[fa[p]]。(mi数组初始时为0)

    这样可以在O(n)更新parent树.

    然后再扫一遍节点,如果mi[p]<len[p],anslen=min(anslen,mi[p]+1),这样就得到了anslen。(anslen为最小串的长度)

    在扫一遍节点把所有可行答案求个字典序最小的就行了。(这一步远远没有O(anslen*|p|),p是可行答案集合的大小,时间复杂度可以看下一个做法的时间)

  2.网上看到的做法

    把其他串建立广义sam,然后让第一个串在上面跑。

    这样每当失配的时候,沿parent树走到符合条件的节点p,则此时最小可行字符串长度为len[fa[p]]+2,拿他去更新答案即可。

    这做法时间复杂度更高,因为字符串比较次数比做法一多(它可能比较了长度大于anslen的字符串),且建立sam的时间复杂度也高。

贴个做法2的代码把,一的不想写了

 #include <bits/stdc++.h>

 using namespace std;

 struct SAM
{
static const int MAXN = <<;//大小为字符串长度两倍
static const int LetterSize = ; int tot, last, ch[MAXN][LetterSize], fa[MAXN], len[MAXN];
int sum[MAXN], tp[MAXN], cnt[MAXN]; //sum,tp用于拓扑排序,tp为排序后的数组 void init( void)
{
last = tot = ;
len[] = ;
memset( ch[], , sizeof ch[]);
} void add( int x)
{
int p = last, np = last = ++tot;
len[np] = len[p] + , cnt[last] = ;
memset( ch[np], , sizeof ch[np]);
while( p && !ch[p][x]) ch[p][x] = np, p = fa[p];
if( p == )
fa[np] = ;
else
{
int q = ch[p][x];
if( len[q] == len[p] + )
fa[np] = q;
else
{
int nq = ++tot;
memcpy( ch[nq], ch[q], sizeof ch[q]);
len[nq] = len[p] + , fa[nq] = fa[q], fa[q] = fa[np] = nq;
while( p && ch[p][x] == q) ch[p][x] = nq, p = fa[p];
}
}
} void toposort( void)
{
for(int i = ; i <= len[last]; i++) sum[i] = ;
for(int i = ; i <= tot; i++) sum[len[i]]++;
for(int i = ; i <= len[last]; i++) sum[i] += sum[i-];
for(int i = ; i <= tot; i++) tp[sum[len[i]]--] = i;
} void go(char *ss)
{
int mi=0x3f3f3f3f,l;
for(int i=,p=,num=,slen=strlen(ss);i<slen;i++)
{
int c=ss[i]-'a';
if(ch[p][c]) p=ch[p][c],num++;
else
{
while(p&&!ch[p][c]) p=fa[p];
if(p)
num=len[p]+,p=ch[p][c];
else
p=,num=;
if(num+<mi) mi=num+,l=i-num;
else if(num+==mi)
for(int j=l,k=i-num;k<=i;k++,j++)
if(ss[j]!=ss[k])
{
if(ss[j]>ss[k]) l=i-num;
break;
}
}
}
if(mi==0x3f3f3f3f)
printf("Impossible");
else
for(int i=;i<mi;i++)
printf("%c",ss[i+l]);
}
} sam; char sa[],sb[];
int main(void)
{
int t,n,cs=;cin>>t;
while(t--)
{
sam.init();
scanf("%d%s",&n,sa);
for(int i=;i<n;i++)
{
scanf("%s",sb);
for(char *p=sb;*p;p++) sam.add(*p-'a');
sam.last=;
}
printf("Case #%d: ",cs++);
sam.go(sa);
printf("\n");
}
return ;
}

[acm/icpc2016ChinaFinal][CodeforcesGym101194] Mr. Panda and Fantastic Beasts的更多相关文章

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

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

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

  3. Gym 101194F Mr. Panda and Fantastic Beasts

    #include<bits/stdc++.h> using namespace std; #define ms(arr,a) memset(arr,a,sizeof arr) #defin ...

  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. hdu6007 Mr. Panda and Crystal 最短路+完全背包

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

  6. 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-中国剩余定理+同余定 ...

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

  8. Gym101194J Mr.Panda and TubeMaster 二分图、费用流

    传送门 看到这张图,是一个网格图,而且有回路限制,不难想到黑白染色. 一般来说我们对一张图黑白染色之后都是黑色点向白色点连边,但是这道题往这边想似乎就想不出建图方法了,因为"一个格子强制流满 ...

  9. Gym 101194C / UVALive 7899 - Mr. Panda and Strips - [set][2016 EC-Final Problem C]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

随机推荐

  1. url 模式录制脚本web_concurrent_start和web_concurrent_end

    LoadRunner函数中文翻译系列之三--Concurrent Groupweb_concurrent_start 语法: int web_concurrent_start ( [char * Co ...

  2. easyui上次图片

    easyuiForm提交: 前台代码: <form id="importFileForm" method="post" enctype="mul ...

  3. 【ArcGIS for Android】基于位置查询Graphic和Feature

    1.graphicsLayer.getGraphicIDs(x, y, 20); 2.featureLayer.getFeatureIDs(x, y, 20); x.y为屏幕坐标 20量纲为dp px ...

  4. Android 网卡地址Mac Wifi文件

    1./system/etc/firmware/ti-connectivity/wl1271-nvs.bin的文件 2./data/etc/wifi/fw文件 3./data/nvram/APCFG/A ...

  5. jqGrid排序的两种实现方式

    实现方案一客户端实现排序: jqGrid属性 loadonce:true时,所有数据加载在客户端,点击列标题由jqGrid在客户端自动排序,不再从服务器取值. 参考文件:ccMxCxTjCc.js j ...

  6. iPhone 6 Screens Demystified

    http://www.paintcodeapp.com/news/iphone-6-screens-demystified

  7. List remove及ConcurrentModificationException异常

    参考:http://blog.csdn.net/androidboy365/article/details/50540202/ 解决方案 // 1 使用Iterator提供的remove方法,用于删除 ...

  8. 170314、工具:apache httpClient多线程并发情况下安全实用及工具类分享

    简单用法介绍:介绍来源网络 建立连接:在HttpClient中使用多线程的一个主要原因是可以一次执行多个方法.在执行期间,每一个方法都使用一个HttpConnection实例.由于在同一时间多个连接只 ...

  9. Storm 在ZK 上的目录图

    这是Zk 的可视化工具 看到的Storm 目录结构 ,这时候没有提交任何的任务给这个集群, 其实这时候我只是启动了 nimbus 还没有启动Supervisors  ,所有你 看懂的Superviso ...

  10. HDU_5510_Bazinga

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...