题目链接:HDU 6208

The Dominator of Strings

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

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

题意:从以上的字符串中找到一个字符串,然后其他所有的字符串都是它的子串。

题目分析:显然若这样的字符串存在就一定是最长的那个,然后考虑下AC自动机的入门题HDU2222,然后思路就清晰了,首先构建自动机,然后选出长度最长的一串来跑一遍自动机,统计所有字符串出现的个数,不重复统计,然后个数!=n就是No。

PS:我自己的模板MLE了TAT,,,随便从网上抓了一个数组建树的模板。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
const int maxm=100010;
const int SIGMA_SIZE=26;
int n;
char t[maxn],s[maxn];struct AC
{int ch[maxm][26];int val[maxm];int fail[maxm],last[maxm];int sz;void clear(){memset(ch[0],0,sizeof(ch[0]));sz=1;}int idx(char x){return x-'a';}void insert(char *s){int u=0;int n=strlen(s);for(int i=0;i<n;i++){int c=idx(s[i]);if(!ch[u][c]){memset(ch[sz],0,sizeof(ch[sz]));val[sz]=0;ch[u][c]=sz++;}u=ch[u][c];}val[u]++;}void getfail(){queue<int> q;fail[0]=0;int u=0;for(int i=0;i<SIGMA_SIZE;i++){u=ch[0][i];if(u){q.push(u);fail[u]=0;last[u]=0;}}while(!q.empty()){int r=q.front();q.pop();for(int i=0;i<SIGMA_SIZE;i++){u=ch[r][i];if(!u){ch[r][i]=ch[fail[r]][i];continue;}q.push(u);int v=fail[r];while(v&&!ch[v][i])v=fail[v];fail[u]=ch[v][i];last[u]=val[fail[u]]?fail[u]:last[fail[u]];}}}int find(char *s){int u=0,cnt=0;int n=strlen(s);for(int i=0;i<n;i++){int c=idx(s[i]);u=ch[u][c];int temp=0;//必须赋初值为0,表示如果下面两个判断都不成立的时候while可以正常执行if(val[u])temp=u;else if(last[u])temp=last[u];while(temp){cnt+=val[temp];val[temp]=0;temp=last[temp];}}return cnt;}
}tree;
int main()
{int T;scanf("%d",&T);while(T--){scanf("%d",&n);int maxlen=0;tree.clear();for(int i=1;i<=n;i++){scanf("%s",t);int lt=strlen(t);if(lt>maxlen){maxlen=lt;strcpy(s,t);}tree.insert(t);}tree.getfail();int ans=tree.find(s);if(ans==n){printf("%s\n",s);}else{printf("No\n");}//printf("%d\n",ans);}return 0;
}

HDU 6208 The Dominator of Strings AC自动机相关推荐

  1. hdu 6208 The Dominator of Strings

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6208 题意:给出n个字符串,判断某一个字符串是否包含了其他所有字符串. 分析:首先这个字符串一定是最长的那 ...

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

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

  3. HDU - 6208 The Dominator of Strings n次KMP 2017 ACM/ICPC Asia Regional Qingdao Online

    找到最长串 然后进行n次KMP #include <iostream> #include <cstdio> #include <cstdlib> #include ...

  4. HDU 3341 Lost's revenge(AC自动机+状态压缩DP)

    Description 给出N个优良的基因段,每段长度小于等于10,只含有AGCT四种碱基. 现给一段基因片段S,|S|<=40.对其重排列后,最多能含有多少个优良基因,基因段可以有公共部分 I ...

  5. HDU 3065病毒侵袭持续中 AC自动机

    题意很明了,就是找每个匹配串在文本中出现的次数,并且根据题意可以可以有重复部分. 所以这个题与板子不同的地方就是查找的一部分. 还有就是多组输入!!! /*┆ ┏┓ ┏┓ ┆┆┏┛┻━━━━━━┛┻┓ ...

  6. CF1202 - E. You Are Given Some Strings...(AC自动机)

    题目链接 题意 1个匹配串TTT,nnn个模式串SSS,求∑i=1n∑j=1nF(T,Si+Sj)\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}F(T,S_i+S ...

  7. HDU - 3065 病毒侵袭持续中(AC自动机)

    题目链接:点击查看 题目大意:给出 n 个模式串和一个文本串,问每个模式串在文本串中分别出现了多少次 题目分析:虽然暴跳fail也是可以实现这个题目的,但个人感觉更好的方法还是建立fail树后在树上d ...

  8. 字典树哇 AC自动机哇 = _ =

    字典树哇 AC自动机哇 = _ = 例题 HDU 1251 统计难题 解题思路 : 字典树 原理:按照每个根向下发散 形成一棵 树 这个题 需要在每一个字母处都做统计 (求前缀单词) 开一个 二维数组 ...

  9. HDU 6208【假AC自动机+string方法】

    题目链接:http://acm.hdu.edu.cn/listproblem.php?vol=1 The Dominator of Strings Time Limit: 3000/3000 MS ( ...

最新文章

  1. 微软忘记修复Mac Office2004/2008安全漏洞
  2. opencv Canny边缘检测用法
  3. java1 lt lt 16,“lt;lt;” Java运算符
  4. 数据预处理-异常值识别
  5. 超市收银程序_超市收银系统案例|千平超市再开2家,星耀助力门店年关创收...
  6. MySQL之INSERT
  7. 回家 Bessie Come Home
  8. C语言斐波那契数列(附完整源码)
  9. 博主已开启评论精选什么意思_小白必看!想要成为小红书博主,首先要掌握4个工具!...
  10. mysql 创建函数_MySQL函数,存储过程,用户管理
  11. 数字图像处理与机器视觉_简单自动智能识别物体程序(机器视觉+数字图像处理)...
  12. 思维转换感悟与区块链视频资料分享
  13. 主流PC常用总线总结
  14. cuteftp下载文件变成macintosh格式
  15. 面试总结:给应届生一些找工作的基本建议,毕竟我踩坑多
  16. windows7系统之家旗舰版下载
  17. 歪门邪道之解决首屏图片加载闪烁问题
  18. 腾讯企业邮箱登录,邮件撤回怎么用?
  19. Rancher 2.x 搭建及管理 Kubernetes 集群
  20. 进制转换和函数的定义

热门文章

  1. (转载)摄影集.西湖边的一棵树
  2. Python实现对相同数据分箱小技巧
  3. CentOS7中基于rpm包方式安装部署apm(php module模块)+ xcache
  4. 戴尔灵越7590 硬件升级 + 散热优化
  5. 人脸识别智能书柜-自助图书借阅柜介绍
  6. bzoj 1933: [Shoi2007]Bookcase 书柜的尺寸
  7. Audio在移动端的兼容性问题(1)
  8. 10倍混合光学变焦+侧旋升降摄像头 OPPO Reno系列新品发布
  9. oracle实战学习记录
  10. 经验分享:java随机生成数字猜大小