HDU 6208:The Dominator of Strings(字符串匹配)
来源:互联网 发布:淘宝店家怎么代销 编辑:程序博客网 时间:2024/06/06 06:39
2017 ACM/ICPC Asia Regional Qingdao Online 1003
The Dominator of Strings
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
题意:
输入n个字符串,问其中是否有字符串能包含其余所有的字串。
解题思路:
首先找到最长的那个串,然后与剩余的所有串匹配,看现场交的情况匹配算法用KMP必然是超时的,百度一下比KMP算法更快的,很自然的就找到了Sunday算法。。。(一下找不到出处就不注明来源了)
Code:
#include<iostream>#include<cstdio>#include<cmath>#include<string>#include<vector>using namespace std;typedef long long int LL;const double EPS=1e-8;int SundaySearch(string text, string pattern){ int i = 0, j = 0, k; int m = pattern.size(); if(pattern.size() <= 0 || text.size() <= 0) return -1; for(; i<text.size();) { if(text[i] != pattern[j]) { for(k=pattern.size() - 1; k>=0; k--) { if(pattern[k] == text[m]) break; } i = m-k; j = 0; m = i+pattern.size(); } else { if(j == pattern.size()-1) return i-j; i++; j++; } } return -1;}vector<string> v;int main(){ ios::sync_with_stdio(false); int T; cin>>T; while(T--) { int n; cin>>n; v.clear(); string t,text; for(int i=0;i<n;i++) { cin>>t; if(text.length()<t.length()) text=t; v.push_back(t); } int flag=1; for(int i=0;i<n;i++) { if(SundaySearch(text,v[i])==-1) { flag=0; break; } } if(flag) cout<<text<<endl; else cout<<"No"<<endl; } return 0;}
- HDU 6208:The Dominator of Strings(字符串匹配)
- HDU 6208 The Dominator of Strings(字符串)
- HDU 6208 The Dominator of Strings
- HDU 6208 The Dominator of Strings
- hdu 6208 The Dominator of Strings
- hdu 6208 The Dominator of Strings
- hdu 6208 The Dominator of Strings
- HDU 6208 The Dominator of Strings
- HDU 6208 The Dominator of Strings(String查找)
- HDU 6208 The Dominator of Strings (kkkkkk....mp)
- HDU 6208 The Dominator of Strings (AC自动机)
- HDU 6208 The Dominator of Strings(stl)
- HDU 6208 The Dominator of Strings (SAM)
- HDU 6208 The Dominator of Strings (字符串find函数暴力过)
- HDU 6208 The Dominator of Strings [AC自动机]
- HDU 6208 The Dominator of Strings AC自动机
- HDU 6208 The Dominator of Strings AC自动机
- HDU 6208 The Dominator of Strings 后缀自动机
- Tools_Obfuscateer_1
- GIT分支管理是一门艺术
- 进程暂停
- 【数据库】模糊查询
- 深入理解 Spring 事务原理
- HDU 6208:The Dominator of Strings(字符串匹配)
- Jenkins修改默认端口号8080
- ROS源代码阅读(2):ROS程序的初始化——从ros:init()出发
- 《MISRA-C 2004 工业标准的C编程规范》-初始化规则
- 第三周项目1---顺序表的基本运算
- Java基础部分第八节
- 枚举
- 泛泛谈SSM和SSH
- Java进阶:垃圾回收机制GC介绍,常见的垃圾回收算法以及垃圾回收与内存分配的关系