地址:

题目:

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. Myeclipse创建Maven项目

    1.右击new创建一个Maven Project: 点击Next 继续Next Finish 2.项目成功创建后,项目目录主要有:主代码目录.主资源目录.测试代码目录.测试资源目录.输出目录(代码编译 ...

  2. 【BZOJ4636】蒟蒻的数列 STL

    [BZOJ4636]蒟蒻的数列 Description 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个数列,初始值均为0,他进行N次操作,每次将数列[a,b)这个 ...

  3. mysql 修改默认配置 提高性能

    解决问题 Lost connection to MySQL server at ‘reading authorization packet’, system error: 0 通过修改 connect ...

  4. Python中的一些函数

    1. 中文繁体/简体转换 下载 zh_wiki.py:https://github.com/skydark/nstools/blob/master/zhtools/zh_wiki.py 和 langc ...

  5. 单台centos7.3 虚拟机实现主从复制和哨兵集群

    环境: centos7.3一台 部署图: 从服务器配置: slaveof 哨兵配置: port sentinel monitor m1 127.0.0.1 6379 2 sentinel monito ...

  6. Vue基础-渲染函数-插槽

    Vue 测试版本:Vue.js v2.5.13 先看个插槽的例子: <div id="app"> <child > <span slot-scope= ...

  7. 03.Curator深入使用

    1.Apache Curator简介     Curator提供了一套Java类库,可以更容易的使用ZooKeeper.ZooKeeper本身提供了Java Client的访问类,但是API太底层,不 ...

  8. 160615、Spring3 MVC 拦截器拦截不到的问题

    昨天项目组有个成员使用拦截器的时候发现不起作用,后来发现了原因,在这里跟大家分享一下(主要是冲突了).分享的是一位网友写的文章,他总结的很好. com.zk.interceptors.MyInterc ...

  9. java generic super, extend

    //Apple Orange 都继承自Fruit类,同时Jonathan是Apple的子类    List<? extends Fruit> list = new ArrayList< ...

  10. 剑指Offer——二叉树的下一个结点

    题目描述: 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 分析: 如果该结点存在右子树,那么返回右子树的最左结 ...