Sdut 2151 Phone Numbers (山东省ACM第一届省赛题 A)
题目描述
We know thatif a phone number A is another phone number B’s prefix, B is not able to becalled. For an example, A is 123 while B is 12345, after pressing 123, we callA, and not able to call B.
Given N phone numbers, your task is to find whether there exits two numbers Aand B that A is B’sprefix.
输入
The input consists of several test cases.
The first line of input in each test case contains one integer N (0<N<1001),represent the number of phone numbers.
The next line contains N integers, describing the phonenumbers.
The last case is followed by a line containing one zero.
输出
For each test case, if there exits a phone number thatcannot be called, print “NO”, otherwise print “YES” instead.
示例输入
2
012
012345
2
12
012345
0
示例输出
NO
YES
/*********************
N个字符串中如果有一个是另外一个的前缀,则输出NO,否则输出YES。
O(n^2)的循环,
*********************/
Code:
#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
char str[1005][1005];
char s[1005];
int n,i,j,li,lj;
bool leaf;
while(scanf("%d",&n)&&n)
{
leaf = false;
li = lj = 0;
for(i = 0;i<n;i++)
cin>>str[i];
for(i = 0;i<n;i++)
{
li = strlen(str[i]);
for(j = 0;j<n;j++)
{
lj = strlen(str[j]);
if(i==j||li>lj)//自己不和自己比较,str[j] 长度比str[i]短的话肯定不是前缀。
continue;
strncpy(s,str[j],li);
s[li] = '\0';
if(strcmp(s,str[i])==0)// 发现前缀则跳出循环
{
leaf = true;
break;
}
}
if(leaf)
break;
}
if(leaf)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
Sdut 2151 Phone Numbers (山东省ACM第一届省赛题 A)的更多相关文章
- ACM Sdut 2158 Hello World!(数学题,排序) (山东省ACM第一届省赛C题)
题目描述 We know thatIvan gives Saya three problems to solve (Problem F), and this is the firstproblem. ...
- Sdut 2164 Binomial Coeffcients (组合数学) (山东省ACM第二届省赛 D 题)
Binomial Coeffcients TimeLimit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 输入 输出 示例输入 1 1 10 2 9 ...
- Sdut 2165 Crack Mathmen(数论)(山东省ACM第二届省赛E 题)
Crack Mathmen TimeLimit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Since mathmen take security ...
- 河南省acm第九届省赛--《表达式求值》--栈和后缀表达式的变形--手速题
表达式求值 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 假设表达式定义为:1. 一个十进制的正整数 X 是一个表达式.2. 如果 X 和 Y 是 表达式,则 X+Y, ...
- sdut 2159:Ivan comes again!(第一届山东省省赛原题,STL之set使用)
Ivan comes again! Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 The Fairy Ivan gave Say ...
- Greatest Number 山东省第一届省赛
Greatest Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Saya likes math, because ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
- 山东省第六届省赛 BIGZHUGOD and His Friends II(赛瓦定理)
Description BIGZHUGOD and his three friends are playing a game in a triangle ground. The number of B ...
- 山东省第七届省赛 D题:Swiss-system tournament(归并排序)
Description A Swiss-system tournament is a tournament which uses a non-elimination format. The first ...
随机推荐
- [SAM4N学习笔记]SAM4N工程模板搭建
一.需要安装的软件: 因为笔者是使用MDK-ARM开发的版本是4.72,所以需要安装这个工具,具体在哪里下载自行放狗或问度娘.除了这个重要工具以为,还需要安装Atmel官方的Atmel Studio, ...
- No valid host was found
set in nova.conf -> scheduler_default_filters=AllHostsFilter Tried without success.
- Jquery Mobile左右滑动效果
首先在一个页面里面定义两个< div data-role="page">,这里为了突出重点,就没有写出footer和header.定义的页面如下: <body&g ...
- Javascript之<script>标签
把javascript代码插入到HTML页面中需要使用<script>标签,使用这个元素可以使javascript和html标记混合在一个页面中,也可以引入外部的javascript文件. ...
- Linux下mysql数据库root无法登录的情况
今天安装lnmp环境后用mysql -u root -p连接密码后出现错误 Access denied for user 'root'@'localhost' (using password: YES ...
- php获取机器网卡的物理(MAC)地址
<?php /** 获取网卡的MAC地址原码:目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址 **/ class GetMacAddr{ var $return_array = ...
- 如何调试PHP的Core之获取基本信息 --------风雪之隅 PHP7核心开发者
http://www.laruence.com/2011/06/23/2057.html https://github.com/laruence PHP开发组成员, Zend兼职顾问, PHP7核心开 ...
- IntelliJ插件安装
1. 插件管理器在线安装 在IntelliJ插件管理页面([FileàSettingsàIDE SettingsàPlugins]),点击[Browse repositories-]按钮,在搜索框内输 ...
- replace()、replaceFirst()和replaceAll()的区别
1.replace() String str= "mesquite in your cellar" str.replace('e', 'o') returns "mosq ...
- PHP程序漏洞产生的原因和防范方法
滥用include 1.漏洞原因: Include是编写PHP网站中最常用的函数,并且支持相对路径.有很多PHP脚本直接把某输入变量作为Include的参数,造成任意引用脚本.绝对路径泄露等漏洞.看以 ...