zoj 2876

#include <vector>
#include <iostream>
#include<algorithm>
#include <string>
#include <cstring>
using namespace std;

int main(){
vector<string> phonelist;
int testcast;
cin >> testcast;
while(testcast--){
phonelist.clear();
int n;
cin >> n;
string str;
for(int i = 0; i < n; i++){
cin >> str;
phonelist.push_back(str);
}
sort(phonelist.begin(), phonelist.end());
// for(int i = 0; i < n; i++){
// cout << phonelist[i] << endl;
// }

int flag = 0;
for(int i = 0; i < n - 1; i++){
if(strcmp(phonelist[i+1].substr(0, phonelist[i].length()).c_str(), phonelist[i].c_str()) == 0)
{
flag = 1;
break;
}
}
if(flag == 0) { cout << "YES" << endl; }
else
{ cout << "NO" << endl; }
}
system("pause");
}

[--李吉环--]<lijihuan0@qq.com> 11:13:57


//zoj/2876

#include<algorithm>
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
vector<string> s;
bool f(string a,string b)
{
int l=a.size()>b.size()?b.size():a.size();
for(int i=0;i<l;i++)
if(a[i]!=b[i])
return false;
return true;
}
int main()
{
int ans;
int test,n;
int i,j;
char ch[100];
scanf("%d",&test);
while(test--)
{
ans=0;
scanf("%d",&n);
s.clear();
while(n--)
{
scanf("%s",ch);
s.push_back(ch);
}
sort(s.begin(),s.end());
for(i=0;i<s.size()-1;i++)
if(f(s[i],s[i+1]))
{
ans=1;
break;
}
printf(ans?"NO ":"YES ");
}
}


*****************************************************************************************************************
新波动(754369601) 11:14:07

zoj 2876:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stdio.h>
using namespace std;
vector <string> my;
vector <string>::iterator it;
int main()
{
int t,n,a,i,j;
int flag;
string s,s1,s2;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
while (n--)
{
cin>>s;
my.push_back(s);
}
sort(my.begin(),my.end());
while (my.size()!=1)
{
flag=1;
s1=my.back();
my.pop_back();
s2=my.back();
a=s2.size()>s1.size()?s1.size():s2.size();
for (j=0;j<a;j++)
if (s2[j]!=s1[j])
{
flag=0;
break;
}
if (flag==1)break;
}
if (flag==0)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
my.clear();
}
return 0;
}

原文地址:https://www.cnblogs.com/2014acm/p/3903238.html