The Dominator of Strings HDU - 6208 (string.find())

传送门

题解:必定从一个最长串中找,这个题不能使用char,会爆,只能使用string,解法有多种,感觉string类型的find函数和char类型的strstr函数好像都不必kmp慢很多。后者网上大神已经验证过了比kmp还快,前者还没证明。找出最长串后,使用string.find()!=string.npos进行匹配


#include<iostream>
#include<cstdio>

using namespace std;

const int maxn=100010;

string s[maxn];

int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        int n,maxn=0,pos=0;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>s[i];
            if(maxn<s[i].size()){
                maxn=s[i].size();
                pos=i;
            }
        }
        int ans=0;
        for(int i=0;i<n;i++){
            if(s[pos].find(s[i])!=string::npos){
                ans++;
            }
        }
        if(ans==n){
            cout<<s[pos]<<endl;
        }else{
            cout<<"No"<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhouzi2018/article/details/81241171