HDU 6208 The Dominator of Strings 后缀自动机
The Dominator of Strings
Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
For each test case, the first line contains an integer N indicating the size of the set.
Each of the following N lines describes a string of the set in lowercase.
The total length of strings in each case has the limit of 100000.
The limit is 30MB for the input file.
10
you
better
worse
richer
poorer
sickness
health
death
faithfulness
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
5
abc
cde
abcde
abcde
bcde
3
aaaaa
aaaab
aaaac
abcde
No
#include <bits/stdc++.h>
inline long long read(){long long x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}return x*f;}
using namespace std; const int N = 2e5+;
const long long mod = ; int isPlus[N * ],endpos[N * ];int d[N * ];
int tot,slink[*N],trans[*N][],minlen[*N],maxlen[*N],pre;
int newstate(int _maxlen,int _minlen,int* _trans,int _slink){
maxlen[++tot]=_maxlen;minlen[tot]=_minlen;
slink[tot]=_slink;
if(_trans)for(int i=;i<;i++)trans[tot][i]=_trans[i];
return tot;
}
int add_char(char ch,int u){
int c=ch-'a',v=u;
int z=newstate(maxlen[u]+,-,NULL,);
isPlus[z] = ;
while(v&&!trans[v][c]){trans[v][c]=z;d[z]+=;v=slink[v];}
if(!v){ minlen[z]=;slink[z]=;return z;}
int x=trans[v][c];
if(maxlen[v]+==maxlen[x]){slink[z]=x;minlen[z]=maxlen[x]+;return z;}
int y=newstate(maxlen[v]+,-,trans[x],slink[x]);
slink[z]=slink[x]=y;minlen[x]=minlen[z]=maxlen[y]+;
while(v&&trans[v][c]==x){trans[v][c]=y;d[x]--,d[y]++;v=slink[v];}
minlen[y]=maxlen[slink[y]]+;
return z;
}
void init_sam() {
for(int i = ; i <= tot; ++i)
for(int j = ; j < ; ++j) trans[i][j] = ;
pre = tot = ;
}
int T,n;
string a[N];
int main() {
ios::sync_with_stdio(false); cin.tie();
cin>>T;
while(T--) {
cin>>n;
int mx = ,flag = ;int ok = ;
for(int i = ; i <= n; ++i) {
cin >> a[i];
if(a[i].length() > mx) {
mx = a[i].length();
flag = i;
}
}
init_sam();
for(int i = ; i < mx; ++i)
pre = add_char(a[flag][i],pre);
for(int i = ; i <= n; ++i) {
if(i == flag) continue;
int now = ;
int p = ;
int ans = ;
for(int j = ; j < a[i].length(); ++j) {
if(trans[p][a[i][j]-'a']) {
now += ;
p = trans[p][a[i][j]-'a'];
}else {
while(p) {
p = slink[p];
if(trans[p][a[i][j]-'a']) break;
}
if(!p) p = ,now = ;
else {
now = maxlen[p] + ;
p = trans[p][a[i][j]-'a'];
}
}
ans = max(ans,now);
}
if(ans != a[i].length()) {
cout<<"No"<<endl;
ok = ;
break;
}
}
if(!ok) cout<<a[flag]<<endl;
}
return ;
}
HDU 6208 The Dominator of Strings 后缀自动机的更多相关文章
- hdu 6208 The Dominator of Strings【AC自动机】
hdu 6208 The Dominator of Strings[AC自动机] 求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了.. #inclu ...
- HDU 6208 The Dominator of Strings(AC自动机)
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- HDU - 6208 The Dominator of Strings HDU - 6208 AC自动机 || 后缀自动机
https://vjudge.net/problem/HDU-6208 首先可以知道最长那个串肯定是答案 然后,相当于用n - 1个模式串去匹配这个主串,看看有多少个能匹配. 普通kmp的话,每次都要 ...
- HDU 6208 The Dominator of Strings【AC自动机/kmp/Sunday算法】
Problem Description Here you have a set of strings. A dominator is a string of the set dominating al ...
- HDU 6208 The Dominator of Strings ——(青岛网络赛,AC自动机)
最长的才可能成为答案,那么除了最长的以外全部insert到自动机里,再拿最长的去match,如果match完以后cnt全被清空了,那么这个最长串就是答案.事实上方便起见这个最长串一起丢进去也无妨,而且 ...
- HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)
Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...
- HDU 5343 MZL's Circle Zhou 后缀自动机+DP
MZL's Circle Zhou Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- Codechef2015 May - Chef and Strings (后缀自动机)
用后缀自动机统计出出现1~n次的串的数量f[i] 对于ans[k]=sigma(f[i]*C(i,k)) i>=k ; mo=; ..maxn] of dword; nt:..maxn,'a'. ...
- Codeforces 452E Three Strings(后缀自动机)
上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...
随机推荐
- 求用delphi编写的LRC校验位算法函数,急!!!
求用delphi编写的LRC校验位算法函数,急!!! 某命令串为":010200000001FC" 其16进制为“3A 30 31 30 32 30 30 30 30 30 30 ...
- 避免在block中循环引用(Retain Cycle in Block)
让我们长话短说.请参阅如下代码: - (IBAction)didTapUploadButton:(id)sender { NSString *clientID = @"YOUR_CLIENT ...
- 如何让自己的程序支持iPhone5–RetinaImages
我们知道如果想让程序的启动图像能够适应iPhone5(640X1136),那么我们需要把启动的图像命名以“-568h@2x”结尾的图片,那 么是不是程序中用到的所有图片都可以用这样的方式来命名,以适合 ...
- python局部变量与全局变量
name = "head first python"def what_happens_here(): print(name) 1 name = "pytho ...
- 【前端GUI】——对一些优秀网页设计作品的分析&心得
前言:优秀的网站设计作品都有一些相似的地方,即使是美学,也一定会遵循着一定的规律. ONE 这一组,属于同类. 主题:点心. 背景:卡通动物形象. 色调:柔和,甜美. 点线面布局: 在这两个页面中,点 ...
- 16. Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52001744 在上一节使用是配置文件的方式进行使用druid,这里在扩散下使用编程式进行使 ...
- Linux内存段的分析
Linux 应用程序的内存分配中,是用 segment(段)进行区别的,使用 size 命令进行查看: size a.out text data bss dec hex filename a. ...
- 时钟展频技术能有效降低EMI,深入讲解展频发生器!
原文地址:https://baijiahao.baidu.com/s?id=1608649367453023659&wfr=spider&for=pc 相关文章: 1.http://b ...
- Android之Intent和Activity
Intent能够说是Android的灵魂,程序跳转和传递数据的时候基本上就是靠Intent了.Intent在Android应用中是相当重要的,理解Intent相应用编程非常有帮助.在Android的官 ...
- Chrome自带恐龙小游戏的源码研究(四)
在上一篇<Chrome自带恐龙小游戏的源码研究(三)>中实现了让游戏昼夜交替,这一篇主要研究如何绘制障碍物. 障碍物有两种:仙人掌和翼龙.仙人掌有大小两种类型,可以同时并列多个:翼龙按高. ...