The Dominator of Strings
Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 497 Accepted Submission(s): 139
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
对于给定的字符串,求是否有一个字符串包括其他所有字符串,如果有就输出这个字符串,如果没有就输出NO
一直是队友在做这个题,它觉得KMP(然而他写错了,一直WA,),这个题描述的太模糊了,字符串的范围也不给,想不懂,最开始我觉得KMP时间复杂度也有点高了,没想到这个题数据比较水,直接用string.find就能过,我觉得可能是除了最长的那个字符串外,其他的字符串都比较短所以find可以解决
代码如下:
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX_V = 100010;
string str[MAX_V];
int pos,n;
bool solve(){
for(int i=1;i<=n;i++){
if(str[pos].find(str[i]) == string::npos)//.find如果没找到返回的是string::npos这个常量,一般是-1
return false;
}
return true;
}
int main(void){
int t;
ios::sync_with_stdio(false);
cin >> t;
while(t--){
cin >> n;
pos = -1;
for(int i=1;i<=n;i++){
cin >> str[i];
if(pos == -1 || str[i].length() > str[pos].length())
pos = i;
}
if(solve())
cout << str[pos] << endl;
else
cout << "No" << endl;
}
return 0;
}