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
题目链接
题目就是要求判断是否存在一个字符串使其它字符串都是它的子串。
首先这个字符串得是最长的,筛选一下然后用ans.find(*it) == ans.npos暴力遍历判断一下。
这道题有大量数据读入,使用cin、cout时最好加上
ios::sync_with_stdio(0);
cin.tie(0);
这个代码使用时scanf和cout不要一起用,printf和cin不要一起用
AC代码:
#include <stdio.h>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
string ans;
cin >> T;
while (T--) {
vector<string> vec;
vector<string>::iterator it;
int n;
cin >> n;
int flag = 1;
ans.clear();
while (n--) {
string str;
cin >> str;
vec.push_back(str);
if (ans.length() < str.length()) {
ans = str;
}
}
for (it = vec.begin();it != vec.end();it++) {
if (ans.find(*it) == ans.npos) {
flag = 0;
break;
}
}
if (flag) {
cout << ans << endl;
}
else {
cout << "No" << endl;
}
}
return 0;
}