当前位置:网站首页>1090.Phone List
1090.Phone List
2022-06-25 14:58:00 【Caramel K】
Description
Given 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 26
In the case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialed the first three digits of bob’s phone number. So this list would not be sonsistent.
Input
The first line of input gives a single integer, 1<=t<=40, the number of test cases. Each test starts with n, the number of phone numbers, on a separate line, 1<=n<=10000. Then follows n linss with one unique phone numbers on each line. A phone number is a sequence of at most ten digits.
Output
For each test case, output “YES” if the list is consistent, or “NO” otherwise.
Sample Input 1
2 3 911 97625999 91125426 5 113 12340 123440 12345 98346
Sample Output 1
NO YES
Their thinking :
At first I used violence to solve problems —— Compare each phone number with the rest , Sure enough, it timed out . To solve the problem of timeout , It needs to be optimized . The optimization method is to sort all the phone numbers according to the dictionary order , Make every two phone numbers have the highest similarity , In this way, we only need to compare two adjacent phone numbers OK La !
I first tried to use char Type two-dimensional array to store these phone numbers , But it will be troublesome when sorting , Because it is very easy to use sort Function can't be used to char Type two-dimensional array sorting ......

So you can set up an array to index each phone number , The code is as follows
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
char a[10005][11];
bool cmp(int x,int y){
return strcmp(a[x],a[y])<0;// Sort in dictionary order
}
int main()
{
int n,m,i,j;
cin>>n;
while(n--){
cin>>m;
int b[m];
for(i=0;i<m;i++){
cin>>a[i];// Save each phone number
b[i]=i;//b Array for sort Index on sorting a The subscript , In this way, we can solve the problem that we can't directly a Array sorting problem
}
sort(b,b+m,cmp);
int l1=strlen(a[b[0]]);// Read the length of the phone number
int flag;
for(i=0;i<m-1;i++){
flag=0;
int l2=strlen(a[b[i+1]]);
for(j=0;j<min(l1,l2);j++){
if(a[b[i]][j]!=a[b[i+1]][j]){
flag=1;// If a phone number is a prefix of another phone number then flag=0, And vice versa 1
break;
}
}
if(flag==0){
break;
}
l1=l2;
}
if(flag==0)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;
}Of course, you don't have to char Array ,C++ Of string Isn't it fragrant? Ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
string a[10005];
bool cmp(string x,string y){
return x<y;
}
int main()
{
int n,m,i;
cin>>n;
while(n--){
cin>>m;
for(i=0;i<m;i++){
cin>>a[i];
}
sort(a,a+m,cmp);
int flag=0;
for(i=0;i<m-1;i++){
int l=min(a[i].size(),a[i+1].size());
if(a[i].substr(0,l)==a[i+1].substr(0,l)){ //substr Is a function that intercepts strings
flag=1;
}
}
if(flag==1)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;
}Does it look simple and easy to understand? Ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha

边栏推荐
- Using Sphinx to automatically generate API documents from py source files
- Get the parameters in the URL and the interchange between parameters and objects
- JS determines whether two values are equal, and compares any two values, including array objects
- Native JS obtains form data and highlights and beautifies JSON output display
- Mutationobserver listens for DOM changes
- JS recursion and while
- 【HBZ分享】LockSupport的使用
- JS to verify whether the string is a regular expression
- Dmsetup command
- Open a restaurant
猜你喜欢

‘make_ unique’ is not a member of ‘std’

Source code analysis of synergetics and ntyco

【Try to Hack】vulhub靶场搭建

Jaspersoft studio adding MySQL database configuration

How to cut the size of a moving picture? Try this online photo cropping tool

Design and implementation of thread pool

New good friend Pinia, leading the new era of state management

Position (5 ways)

开餐馆

电源自动测试系统NSAT-8000,精准高速可靠的电源测试设备
随机推荐
QT opens the print dialog box in a text editor
Clipboard tutorial
Source code analysis of synergetics and ntyco
有哪个瞬间让你觉得这个世界出bug了?
[untitled] PTA check password
QT file reading -qfile
网上股票开户安不安全?有谁知道呢
电源自动测试系统NSAT-8000,精准高速可靠的电源测试设备
Usage of pure virtual functions
High precision addition
One question per day, a classic simulation question
Study notes of cmake
【深度学习】多任务学习 多个数据集 数据集漏标
QT inline dialog
定位position(5种方式)
Power automatic test system nsat-8000, accurate, high-speed and reliable power test equipment
Usage of qlist
p1408
JS get the height and width corresponding to the box model (window.getcomputedstyle, dom.getboundingclientrect)
Jaspersoft studio adding MySQL database configuration