山东省第一届ACM大学生程序设计竞赛 Phone Number 字典树
来源:互联网 发布:万方—中国机构数据库 编辑:程序博客网 时间:2024/06/09 18:44
Phone Number
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
We know that if a phone number A is another phone number B’s prefix, B is not able to be called. For an example, A is 123 while B is 12345, after pressing 123, we call A, and not able to call B.
Given N phone numbers, your task is to find whether there exits two numbers A and B that A is B’s prefix.
Given N phone numbers, your task is to find whether there exits two numbers A and B that A is B’s prefix.
输入
The input consists of several test cases.
The first line of input in each test case contains one integerN (0<N<1001), represent the number of phone numbers.
The next line containsN integers, describing the phone numbers.
The last case is followed by a line containing one zero.
The first line of input in each test case contains one integerN (0<N<1001), represent the number of phone numbers.
The next line containsN integers, describing the phone numbers.
The last case is followed by a line containing one zero.
输出
For each test case, if there exits a phone number that cannot be called, print “NO”, otherwise print “YES” instead.
示例输入
20120123452120123450
示例输出
NOYES
提示
来源
2010年山东省第一届ACM大学生程序设计竞赛
水题字典树,判断是否有前缀号码出现
ACcode:
#include <map>#include <queue>#include <cmath>#include <cstdio>#include <cstring>#include <stdlib.h>#include <iostream>#include <algorithm>#define MAX 10#define maxn 1005using namespace std;struct Trie{ Trie* next[MAX]; int v;};struct STR{ char str[maxn]; int len; bool operator<(const STR &a)const{ return (this->len<a.len); }}my[maxn];Trie *root;int n,ans,num;void createTire(char *str){ int len=strlen(str); Trie *p=root,*q; for(int i=0;i<len;++i){ int id=str[i]-'0'; if(p->next[id]==NULL){ q=(Trie *)malloc(sizeof(Trie)); q->v=1; for(int j=0;j<MAX;++j) q->next[j]=NULL; p->next[id]=q; p=p->next[id]; } else{ p->next[id]->v++; p=p->next[id]; } } p->v=-1;}int findTrie(char *str){ int len=strlen(str); Trie *p=root; for(int i=0;i<len;++i){ int id=str[i]-'0'; p=p->next[id]; if(p==NULL) return 0; if(p->v==-1) return 0; } return 2;}void init(){ root=(Trie *)malloc(sizeof(Trie)); for(int i=0;i<MAX;++i) root->next[i]=NULL; ans=num=0; memset(my,0,sizeof(my));}int main(){ while(~scanf("%d",&n)&&n){ init(); for(int i=0;i<n;++i){ scanf("%s",my[i].str); my[i].len=strlen(my[i].str); } sort(my,my+n); for(int i=0;i<n;++i)createTire(my[i].str); // for(int i=0;i<n;i++)cout<<my[i].str<<'\12'; for(int i=0;i<n;++i) ans+=findTrie(my[i].str); //cout<<"ans: "<<ans<<'\12'; for(int i=0;i<n&&!ans;++i) for(int j=i+1;j<n;++j) if(!strcmp(my[i].str,my[j].str)) ans++; printf(ans>0?"NO\n":"YES\n"); } return 0;}/*20120123452120123450*/
0 0
- 山东省第一届ACM大学生程序设计竞赛 Phone Number 字典树
- Phone Number 2010年山东省第一届ACM大学生程序设计竞赛
- [2010山东省第一届ACM大学生程序设计竞赛]——Phone Number
- SDUT 2151 Phone Number 2010年山东省第一届ACM大学生程序设计竞赛
- Phone Number 山东省第一届ACM大学生程序设计竞赛(原题) problem A
- 山东省第一届ACM大学生程序设计竞赛--Greatest Number--二分
- [2010山东省第一届ACM大学生程序设计竞赛]——Greatest Number
- 2010年山东省第一届ACM大学生程序设计竞赛——Greatest Number
- SDUT 2157 Greatest Number (二分) 2010年山东省第一届ACM大学生程序设计竞赛
- 山东省第一届ACM大学生程序设计竞赛 Greatest Number 二分查找
- 山东省第一届ACM大学生程序设计竞赛 problem D Greatest Number
- 山东省第一届ACM大学生程序设计竞赛 Shopping 水。。。。。。。
- 山东省第一届ACM大学生程序设计竞赛 Balloons bfs搜索
- 山东省第一届ACM大学生程序设计竞赛-Balloons(搜索)
- 2010年山东省第一届ACM大学生程序设计竞赛:shopping
- 2010年山东省第一届ACM大学生程序设计竞赛:Balloons
- 山东省第一届ACM大学生程序设计竞赛 Emergency floyd变形
- 山东省第一届ACM大学生程序设计竞赛 problemG Shopping
- 第5周项目4:长方柱类
- 面试题70:二叉搜索树第K大的结点*
- 设计模式 概述
- 浏览器兼容笔记(2)
- Oracle常用命令大全
- 山东省第一届ACM大学生程序设计竞赛 Phone Number 字典树
- git命令总结
- js获取Url中参数值
- iOS开发之tableViewCell高度自适应
- C语言经典小程序(热门题型)
- android 自动初始化控件 不用注解不用findViewByid
- 百度地图为啥定位不了
- 支持向量机—线性可分支持向量机与硬间隔最大化
- 第四周项目3①-小学生算术能力测试系统