天天看點

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;
}