HDOJ1671(字符串前缀匹配)

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector<string> words;
bool isPrefix(string s1, string s2)
{
    string::iterator it1=s1.begin();
    string::iterator it2=s2.begin();
    while(it1!=s1.end()&&it2!=s2.end())
    {
        if((*it1)!=(*it2))
        {
            return false;
        }
        it1++;
        it2++;
    }
    return true;
}

int main()
{
    int t;cin>>t;
    while(t--)
    {
        words.clear();
        int n;cin>>n;
        for(int i=0; i<n ;i++)
        {
            string s;cin>>s;
            words.push_back(s);
        }
        
        sort(words.begin(), words.end());
        int flag=false;
        for(int i=1; i<n; i++)
        {
            if(isPrefix(words[i],words[i-1]))
            {
                flag=true;
                break;
            }
        }
        if(flag)
        {
            cout<<"NO"<<endl;
        }
        else
        {
            cout<<"YES"<<endl;
        }
    }
    
    return 0;
}
原文地址:https://www.cnblogs.com/program-ccc/p/4769227.html