2018TYUT暑期ACM模拟赛(8)
​​​The Dominator of Strings HDU - 6208​​​
题意:给你n个字符串找到一个字符串,可以包含给的所有串,如果存在就输出该字符串,不存在就输出No。
思路:意外暴力过,我的做法直接按照字符串的长度从大到小的排序。只需要找最长的是否包含全部串即可,这里用了string自带的函数find()。

find()函数用法:
返回str在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos,
返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,
返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos
例如,

string str1( "Alpha Beta Gamma Delta" );
unsigned int loc = str1.find( "Omega", 0 );
if( loc != string::npos )
cout << "Found Omega at " << loc << endl;
else
cout << "Didn't find Omega" << endl;
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int maxn=100000+10;
string s[maxn];
struct node
{
string s;
int len;
bool operator <(const node &u)const{
return len>u.len;
}
}nodes[maxn];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
cin>>nodes[i].s;
nodes[i].len=nodes[i].s.size();
}
sort(nodes,nodes+n);
bool flag=0;
for(int i=1;i<n;i++)
{
if(nodes[0].s.find(nodes[i].s)==string::npos)
{
flag=1;
break;
}
}
if(flag) printf("No\n");
else cout<<nodes[0].s<<endl;
}
return 0;
}