Sicily 1426. Phone List 解题报告

本文介绍了一种用于检查电话号码列表一致性的算法。通过排序并逐一对比相邻号码,判断是否存在一个号码作为另一个号码的前缀。算法适用于1到40个测试案例,每个案例包含1到10000个电话号码。

题目:

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 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.

Input

The 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.

Output

For each test case, output “YES” if the list is consistent, or “NO” otherwise.

Sample Input

2
3
911
97625999
91125426
5
113
12340
123440
12345
98346

Sample Output

NO
YES

 

 思路:

用string 类型数组读进所有电话号码,然后用sort函数对其进行排序。

排序与每个号码的长短无关(前面相同的较短的较小),而是从首位开始逐位比较。

如对13,123,124,11456使用sort函数进行排序,结果会是11456,123,124,13

这样排序之后如果有不相容的两个号码肯定会出现在相邻的两位了,所以对整个数组所有相邻的两位逐位进行比对即可得到结果。

 

 

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 int main(){
 6     int testcases;
 7     cin>>testcases;
 8     while(testcases--){
 9         int num_of_phone_number;
10         bool consistent=true;//If two numbers are found inconsistent,it will be false
11         cin>>num_of_phone_number;
12         string numbers[num_of_phone_number];
13         for(int i=0;i<num_of_phone_number;i++){
14             cin>>numbers[i];
15         }
16         sort(numbers,numbers+num_of_phone_number);
17         //Now if inconsistent numbers exist,they must be adjacent
18         for(int i=0;i<num_of_phone_number-1;i++){//linear search every two numbers
19             int len=numbers[i].length();
20             bool match=true;
21             for(int j=0;j<len;j++){
22                 if(numbers[i][j]!=numbers[i+1][j]){
23                     match=false;
24                     break;
25                 }
26             }
27             if(match){
28                 cout<<"NO"<<endl;
29                 consistent=false;
30                 break;
31             }
32         }
33         if(consistent)
34             cout<<"YES"<<endl;
35     }
36     return 0;
37 }

 

 

转载于:https://www.cnblogs.com/jolin123/p/3405730.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值