地址:

题目:

The Dominator of Strings

Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 857    Accepted Submission(s): 264

Problem Description
Here you have a set of strings. A dominator is a string of the set dominating all strings else. The string S is dominated by T if S is a substring of T.
 
Input
The input contains several test cases and the first line provides the total number of cases.
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.
 
Output
For each test case, output a dominator if exist, or No if not.
 
Sample Input
3
10
you
better
worse
richer
poorer
sickness
health
death
faithfulness
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
5
abc
cde
abcde
abcde
bcde
3
aaaaa
aaaab
aaaac
 
Sample Output
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
abcde
No
 
Source
 
思路:
  这题数据好像很水,做法多的飞起:ac自动机能过,后缀自动机能过,kmp也能过(好像有特殊技巧才可以),暴力hash也能过。
  我是ac自动机做的,看题后写完1a了,还以为是sb题,发现我也是跑了2800ms,也是卡过去的,还好提前关了同步。
  ac自动机做法就是,把最长串以外的串建立ac自动机,然后让最长串在上面跑就好了。
  讲道理这复杂度也就是:t*所有串的总长度=t*1e5,常数大概20或30左右吧(瞎分析)
  ps:赛后测了下同一份也就跑了2000ms,比赛时却跑了2800ms,这服务器。。。。又交了几发,发现服务器不稳定,一般是2200ms左右
  再讲个后缀自动机做法:把最长串串建立sam,然后让其他串在上面跑就好了,这个做法快的飞起,跑了560ms
ac自动机代码:
 #include <queue>
#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
using namespace std; struct AC_auto
{
const static int LetterSize = ;
const static int TrieSize = * ( 1e5 + ); int tot,root,fail[TrieSize],end[TrieSize],next[TrieSize][LetterSize]; int newnode(void)
{
memset(next[tot],-,sizeof(next[tot]));
end[tot] = ;
return tot++;
} void init(void)
{
tot = ;
root = newnode();
} int getidx(char x)
{
return x - 'a';
} void insert(string &ss)
{
int len = ss.length();
int now = root;
for(int i = ; i < len; i++)
{
int idx = getidx(ss[i]);
if(next[now][idx] == -)
next[now][idx] = newnode();
now = next[now][idx];
}
end[now]++;
} void build(void)
{
queue<int>Q;
fail[root] = root;
for(int i = ; i < LetterSize; i++)
if(next[root][i] == -)
next[root][i] = root;
else
fail[next[root][i]] = root,Q.push(next[root][i]);
while(Q.size())
{
int now = Q.front();Q.pop();
for(int i = ; i < LetterSize; i++)
if(next[now][i] == -) next[now][i] = next[fail[now]][i];
else
fail[next[now][i]] = next[fail[now]][i],Q.push(next[now][i]);
}
} int match(string &ss)
{
int len,now,res;
len = ss.length(),now = root,res = ;
for(int i = ; i < len; i++)
{
int idx = getidx(ss[i]);
int tmp = now = next[now][idx];
while(tmp)
{
res += end[tmp];
end[tmp] = ;//按题目修改
tmp = fail[tmp];
}
}
return res;
}
void debug()
{
for(int i = ;i < tot;i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
for(int j = ;j < LetterSize;j++)
printf("%3d",next[i][j]);
printf("]\n");
}
}
};
AC_auto ac;
string ss[];
int main(void)
{
std::ios::sync_with_stdio();
cin.tie();
//freopen("in.acm","r",stdin);
int t,n,id;cin>>t;
while(t--)
{
id=;
ac.init();
cin>>n>>ss[];
for(int i=;i<=n;i++) cin>>ss[i],id=ss[id].length()>ss[i].length()?id:i;
for(int i=;i<=n;i++)
if(i!=id)
ac.insert(ss[i]);
ac.build();
if(ac.match(ss[id])==n-)
cout<<ss[id]<<"\n";
else
cout<<"No\n";
}
return ;
}

sam做法:

 #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[]);
memset(cnt,,sizeof cnt);
} 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;
for(int i = tot; i; i--) cnt[fa[tp[i]]] += cnt[tp[i]];
} bool match(string &s)
{
for(int i=,p=;i<s.length();p=ch[p][s[i]-'a'],i++)
if(!ch[p][s[i]-'a'])
return ;
return ;
}
} sam;
string ss[];
int main(void)
{
ios::sync_with_stdio();
cin.tie();
int t;cin>>t;
while(t--)
{
int n,ff=,id=;
cin>>n;
for(int i=;i<=n;i++) cin>>ss[i],id=(ss[i].length()>ss[id].length()?i:id);
sam.init();
for(int i=;i<=n;i++)
if(i!=id)
{
for(int j=;j<ss[i].length();j++) sam.add(ss[i][j]-'a');
sam.add();
}
for(int i=n;i&&ff;i--)
if(i!=id&&!sam.match(ss[i]))
ff=;
if(ff) cout<<ss[id]<<"\n";
else cout<<"No\n";
}
return ;
}
 

hdu6208 The Dominator of Strings的更多相关文章

  1. hdu 6208 The Dominator of Strings【AC自动机】

    hdu 6208 The Dominator of Strings[AC自动机] 求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了.. #inclu ...

  2. HDU 6208 The Dominator of Strings(AC自动机)

    The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java ...

  3. HDU 6208 The Dominator of Strings 后缀自动机

    The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java ...

  4. 2017 ACM/ICPC Asia Regional Qingdao Online 1003 The Dominator of Strings hdu 6208

    The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java ...

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

  6. HDU - 6208 The Dominator of Strings HDU - 6208 AC自动机 || 后缀自动机

    https://vjudge.net/problem/HDU-6208 首先可以知道最长那个串肯定是答案 然后,相当于用n - 1个模式串去匹配这个主串,看看有多少个能匹配. 普通kmp的话,每次都要 ...

  7. The Dominator of Strings HDU - 6208(ac自动机板题)

    题意: 就是求是否有一个串 是其它所有串的母串 解析: 把所有的串都加入到trie数中  然后用最长的串去匹配就好了 emm..开始理解错题意了...看成了只要存在一个串是另一个的母串就好.. 然后输 ...

  8. 【hdu 6208】The Dominator of Strings

    [链接]h在这里写链接 [题意] 问你n个串里面有没有一个串,使得其余n-1个串都是他的子串. [题解] 后缀数组. 答案肯定是那个最长的串. 则,把那个串求一下Sa数组(注意仅仅那个最长的串求). ...

  9. HDU 6208 The Dominator of Strings ——(青岛网络赛,AC自动机)

    最长的才可能成为答案,那么除了最长的以外全部insert到自动机里,再拿最长的去match,如果match完以后cnt全被清空了,那么这个最长串就是答案.事实上方便起见这个最长串一起丢进去也无妨,而且 ...

随机推荐

  1. SQLserver 设置自增为显式插入

    默认是状态是set IDENTITY_INSERT T2 off ,就是关闭了自动插入值的功能,为空时就会报错 ,,'') 报错: 消息 544,级别 16,状态 1,第 1 行当 IDENTITY_ ...

  2. php7 宏杂记

    php.h                                 zend_api.h ZEND_FN(name) ---> zif_##name PHP_FUNCTION(name) ...

  3. 简单配置Nginx 指向本地端口,并开启SSL

    简单配置Nginx 指向本地端口,并开启SSL,如果要开启SSL,必须使用域名去申请SSL key,一般是两个文件,一般是要收费的. # 在/etc/nginx/nginx.conf 的文件中有下面一 ...

  4. C# winform 中MessageBox用法大全(附效果图)

    我们在程序中经常会用到MessageBox. MessageBox.Show()共有21中重载方法.现将其常见用法总结如下: 1.MessageBox.Show(“Hello~~~~”); 最简单的, ...

  5. WinForm------如何修改PanelControl控件背景色

    方法: 如何修改panelcontrol背景色 this.bottomPanel.BorderStyle = BorderStyles.NoBorder; this.bottomPanel.Appea ...

  6. SQLServer------数据类型在C#中的转换

    SQL C#tinyint bytesmallint Int16char stringint int

  7. Linux GCC编译使用动态、静态链接库 (转)

    原文出处:http://blog.csdn.net/a600423444/article/details/7206015 在windows下动态链接库是以.dll后缀的文件,二在Linux中,是以.s ...

  8. 第六篇:二维数组的传输 (host <-> device)

    前言 本文的目的很明确:介绍如何将二维数组传递进显存,以及如何将二维数组从显存传递回主机端. 实现步骤 1. 在显存中为二维数组开辟空间 2. 获取该二维数组在显存中的 pitch 值 (cudaMa ...

  9. 如何通过Keil将程序正确的下载进flash中

    前面介绍了一些创建工程和调试的基本步骤,在这里准备介绍一下如何正确的将Keil程序在仿真调试中下载到flash.这里再次涉及到了debug的窗口.   工具/原料   Keil uVision 4/5 ...

  10. Struts2漏洞利用原理及OGNL机制

    Struts2漏洞利用原理及OGNL机制研究   概述 在MVC开发框架中,数据会在MVC各个模块中进行流转.而这种流转,也就会面临一些困境,就是由于数据在不同MVC层次中表现出不同的形式和状态而造成 ...