首页 > 学院 > 开发设计 > 正文

x-24

2019-11-06 06:30:06
字体:
来源:转载
供稿:网友
题目描述:DescriptionGiven a list of phone numbers, determine if it is consistent in the sense that no number is the PRefix of another. Let's say the phone catalogue listed these numbers:- Emergency 911- Alice 97 625 999- Bob 91 12 54 26In this case, it's not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.InputThe first line of input gives a single integer, 1 <= t <= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 <= n <= 10000.Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.OutputFor each test case, output "YES" if the list is consistent, or "NO" otherwise.Sample Input2391197625999911254265113123401234401234598346Sample OutputNOYES简单概述:即给你几个字符串,让你寻找是否有包含的关系,即某一字符串是不是为另一字符串的子串。解题思路:先将给出的字符串进行排序,然后寻找是否有包含的,注意运用标志flag和采用find函数。细节处理:由于习惯性问题,将寻找过程中的j写成了i,导致不能AC,并且出现segmentation fault的错误。

代码:

#include<bits/stdc++.h>using namespace std;int main(){	vector<string>v;	string s;	int m,n,i,j,k;	cin>>n;	for(i=0;i<n;i++)	{		int flag=0;		v.clear();		cin>>m;		for(j=0;j<m;j++)		{			cin>>s;			v.push_back(s);//压入向量中		}		sort(v.begin(),v.end());		for(j=0;j<m-1;j++)		{			if(v[j+1].find(v[j])==0)//寻找是否包含子串			{				flag++;//建立的标志				}			}			 if(flag==0)			 cout<<"YES"<<endl;			 else       cout<<"NO"<<endl;		}			return 0;	}心得体会:

做题应先想清楚在敲代码,注意一定要仔细!避免低级性错误!


上一篇:HDU2519

下一篇:spring整合struts2

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表