The Dominator of Strings
来源:互联网 发布:mac os的硬件配置要求 编辑:程序博客网 时间:2024/06/01 21:03
Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 515 Accepted Submission(s): 146
Problem Description
Here you have a set of strings. A dominator is a string of the set dominating all strings else. The stringS 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 integerN indicating the size of the set.
Each of the followingN lines describes a string of the set in lowercase.
The total length of strings in each case has the limit of100000 .
The limit is 30MB for the input file.
For each test case, the first line contains an integer
Each of the following
The total length of strings in each case has the limit of
The limit is 30MB for the input file.
Output
For each test case, output a dominator if exist, or No if not.
Sample Input
310youbetterworsericherpoorersicknesshealthdeathfaithfulnessyoubemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness5abccdeabcdeabcdebcde3aaaaaaaaabaaaac
Sample Output
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulnessabcdeNo
Source
2017 ACM/ICPC Asia Regional Qingdao Online
唉 ,,,,好坑啊,开始用ac自动机,要不超内存,要不就超时,挑了将近两三个小时,没ac,比赛结束,听说用kmp就能过,然后试了下,AC
代码如下:
#include <iostream> #include <cstdio> #include <stdlib.h>#include <cstring> #include <queue> using namespace std;int kmp_find(const char* target,const char* pattern){ const int target_length = strlen(target); const int pattern_length = strlen(pattern); int * overlay_value = new int[pattern_length]; overlay_value[0] = -1;//存前缀和后缀相同元素的长度 int index = 0; for(int i=1;i<pattern_length;++i) { index = overlay_value[i-1]; while(index>=0 && pattern[index+1]!=pattern[i]) { index = overlay_value[index]; } if(pattern[index+1]==pattern[i]) { overlay_value[i] = index +1; } else { overlay_value[i] = -1; } } //match algorithm start int pattern_index = 0; int target_index = 0; while(pattern_index<pattern_length&&target_index<target_length) { if(target[target_index]==pattern[pattern_index]) { ++target_index; ++pattern_index; } else if(pattern_index==0) { ++target_index; } else { pattern_index = overlay_value[pattern_index-1]+1; } } delete [] overlay_value; if(pattern_index==pattern_length) { return target_index-pattern_index; } else { return -1; } } char s[200002];char* p[100005];int main() { int t,n,m,len,i; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(s,'\0',sizeof(s)); len=m=0; p[0]=s; for(int i=0;i<n;i++) { scanf("%s",p[i]); if(strlen(p[i])>len){len=strlen(p[i]);m=i;} p[i+1]=p[i]+strlen(p[i])+1; } int ans=0;for(int i=0;i<n;i++){if(i!=m&&kmp_find(p[m],p[i])!=-1)ans++;}if(ans==n-1)printf("%s\n",p[m]);elseprintf("No\n"); } return 0; }
阅读全文
0 0
- The Dominator of Strings
- The Dominator of Strings
- 1003 The Dominator of Strings
- HDU6208 The Dominator of Strings
- HDU 6208 The Dominator of Strings
- HDU6208 The Dominator of Strings【字符串】
- HDU 6208 The Dominator of Strings
- hdu 6208 The Dominator of Strings
- 【KMP算法】The Dominator of Strings HDU
- The Dominator of Strings(杭电icpc)
- hdu 6208 The Dominator of Strings
- hdu 6208 The Dominator of Strings
- HDU 6208 The Dominator of Strings
- hdoj 6208 The Dominator of Strings
- HDU 6208 The Dominator of Strings [AC自动机]
- 2017ICPC青岛网络赛-The Dominator of Strings
- HDU 6208 The Dominator of Strings AC自动机
- HDU 6208:The Dominator of Strings(字符串匹配)
- Java接口回调的两种使用
- 9. Palindrome Number
- HDU 2306 求多边形面积
- 计算机网络整理
- Linux学习(十三):Makefile
- The Dominator of Strings
- Sata接口读取新硬盘读不出问题解决
- js中几种实用的跨域方法原理详解
- 9.17【青岛ACM胡玩记】
- 多线程的优点
- Java HashMap 储存自定义对象
- 简单ScrollView
- LeetCode38
- hdu6208