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这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...
随机推荐
- hdu 4517(递推枚举统计)
小小明系列故事——游戏的烦恼 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- JS-JavaScript String 对象-string对象方法3:concat()
1.concat():用于连接两个或多个字符串. 1).语法:string.concat(string1, string2, ..., stringX) (string1, string2, . ...
- win10+anaconda3+pytorch安装
安装条件:win10+anaconda3+tf-gpu+pytorch 安装完tf-gpu后,安装pytorch. pytorch采用conda安装:见 https://ptorch.com/news ...
- 51nod 1105 第K大的数 【双重二分/二分套二分/两数组任意乘积后第K大数】
1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...
- SecureCRT分屏显示
[Tab右键]或者[Session Manager右键]->[Send to New Tab Group]
- Putty完全使用方法
http://www.putty.ws/Putty-wanquanshiyong putty中文站
- redis--服务器与客户端
初始化服务器 从启动 Redis 服务器,到服务器可以接受外来客户端的网络连接这段时间,Redis 需要执行一系列初始化操作. 整个初始化过程可以分为以下六个步骤: 初始化服务器全局状态. 载入配置文 ...
- 新学到的linux命令
whatis xxx whatis ls 会返回一个ls的简要说明,可以简单的告诉你该命令的作用,不用man去看一大堆没啥用的英文 HISTTIMEFORMAT="%d/%m/%y %T & ...
- 2017.2.12 开涛shiro教程-第八章-拦截器机制
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 1.拦截器介绍 下图是shiro拦截器的基础类图: 1.Namea ...
- 微信小程序 - 考试前三排名实现
实现原理:利用背景图片以及nth-child实现