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这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...
随机推荐
- mysql5.7 mysql库下面的user表没有password字段无法修改密码
如题所述,mysql5.7 mysql库下面的user表没有password字段无法修改密码, 5.7版本已经不再使用password来作为密码的字段了 而改成了authentication_st ...
- LeetCode OJ-- Valid Sudoku
https://oj.leetcode.com/problems/valid-sudoku/ 给出数独的一部分来,验证给出的这一部分是否为 valid 行上无重复的,且是从 ‘1’ 到‘9’ 列上无重 ...
- 内存 : CL设置
CL(CAS Latency):为CAS的延迟时间,这是纵向地址脉冲的反应时间,也是在一定频率下衡量支持不同规范的内存的重要标志之一. 内存负责向CPU提供运算所需的原始数据,而目前CPU运行速度超过 ...
- HDU 5869.Different GCD Subarray Query-区间gcd+树状数组 (神奇的标记右移操作) (2016年ICPC大连网络赛)
树状数组... Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/6 ...
- 不一样视角的Glide剖析
推荐阅读: 滴滴Booster移动App质量优化框架-学习之旅 一 Android 模块Api化演练 不一样视角的Glide剖析(一) Glide是一个快速高效的Android图片加载库,注重于平滑的 ...
- BZOJ 4197 NOI 2015 寿司晚宴
题面 Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了 n−1 ...
- Spring事务管理——事务的传播行为
1.简介 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播.例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己的事务中运行. 事务的传播行为可以由传播属性指定.Spring定 ...
- 用NSLogger代替NSLog输出调试信息
安装 NSLogger分为两部分,LoggerClient和NSLogger Viewer,你的App需要导入前者,后者是一个独立的mac应用,NSLogger所有的调试信息将输出到这个应用中. 安装 ...
- 获取安装后Apache、MySQL、Nginx、PHP编译时参数
# cat /usr/local/apache2/build/config.nice //获取Apache编译时的参数 #!/bin/sh # #Created by configure & ...
- 负载均衡情况下获取真实ip的方法
公司用了硬件负载均衡,最近发现日志中的用户ip都为负载均衡器的ip,业务需要所以要改为用户真实ip,下面记录一下! 1.打开文件:/etc/httpd/conf/httd.conf. 2.在文件中查找 ...