1
Here you have a set of strings. A dominator is a string of the set dominating all strings else. The string
S
S is dominated by
T
T if
S
S is a substring of
T
T.
For each test case, the first line contains an integer N N indicating the size of the set.
Each of the following N 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.
3 10 you better worse richer poorer sickness health death faithfulness youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness 5 abc cde abcde abcde bcde 3 aaaaa aaaab aaaac
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness abcde No
题目大意: 给你 n 个字符串,让你输出其中一个字符串,这个字符串包含所有其他的字符串。如果没有,输出 No ;
字符串的长度小于100000,个数 n 没有范围,但输入的总量 不超过 30Mb。
思路:由于n不确定,所以可以用vector 来存放字符串。(set也可以,但是由于他会在容器中排序,所以时间会多一点)。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
vector<string> st;
int main ()
{
std::cin.sync_with_stdio(false);
int T;
cin>>T;
while(T--)
{
int n,i,mxlen=-1,bj=0;
string mx;
st.clear();
cin>>n;
for(i=0;i<n;i++)
{
string s;
cin>>s;
st.push_back(s);
}
for(vector<string>::iterator it=st.begin();it!=st.end();it++)
{
int tmp;
tmp=(*it).length();
if(tmp>mxlen)
{
mxlen=tmp;
mx=*it;
}
}
for(vector<string>::iterator it=st.begin();it!=st.end();it++)
{
int tmp,j,k;
tmp=(*it).length();
if(tmp==mxlen)
{
if(*it!=mx)
{
bj++;
}
}
if(mx.find(*it)==string::npos)
bj++;
if(bj!=0)
{
cout<<"No"<<endl;
break;
}
}
if(bj==0)
cout<<mx<<endl;
}
return 0;
}