POJ2001Shortest Prefixes[Trie]
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 17683 | Accepted: 7686 |
Description
In the sample input below, "carbohydrate" can be abbreviated to "carboh", but it cannot be abbreviated to "carbo" (or anything shorter) because there are other words in the list that begin with "carbo".
An exact match will override a prefix match. For example, the prefix "car" matches the given word "car" exactly. Therefore, it is understood without ambiguity that "car" is an abbreviation for "car" , not for "carriage" or any of the other words in the list that begins with "car".
Input
Output
Sample Input
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate
Sample Output
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona
Source
//
// main.cpp
// poj2001
//
// Created by Candy on 10/13/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N=1e4+;
int t[N*][],val[N*],sz;
int n=;
char s[N][];
inline int id(char c){return c-'a';}
void insert(char a[]){
int len=strlen(a+),u=;
for(int i=;i<=len;i++){
int c=id(a[i]);
if(!t[u][c]) t[u][c]=++sz;
u=t[u][c];
val[u]++;
}
}
void query(char a[]){
int len=strlen(a+),u=;
for(int i=;i<=len;i++){
int c=id(a[i]);
if(val[u]==) break;
putchar(a[i]);
u=t[u][c];
}
putchar('\n');
}
int main(int argc, const char * argv[]) {
while(scanf("%s",s[++n]+)!=EOF) insert(s[n]);
for(int i=;i<=n;i++){
printf("%s ",s[i]+);
query(s[i]);
}
return ;
}
POJ2001Shortest Prefixes[Trie]的更多相关文章
- poj2001Shortest Prefixes(trie)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18687 Accepted: 808 ...
- POJ2001Shortest Prefixes(Trie树)
传送门 题目大意:求最短唯一前缀 题解:Trie树 把单词一个个插入,每个字母节点v[]++;然后输出时输出到v[]为1的点, v[]=1说明只有这个单词经过. 代码 : #include<io ...
- poj 2001 Shortest Prefixes trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
- POJ 2001 Shortest Prefixes (Trie)
题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...
- poj2001 Shortest Prefixes (trie树)
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- POJ2001Shortest Prefixes(字典树)
题目大意就是帮你给N条字符串,每条长度不超过20.问要将他们单一识别出来,每个字符串最短可以缩为多短. 如: abc abcdefg bc adef 这四个字符串就可分别缩写为 abc abcd b ...
- POJ 2001-Shortest Prefixes(Trie 入门)
题意:给你一组串,求每个串在组中唯一标志的最短前缀 分析:保存树上经过各节点的单词个数,扫描每个串当经过节点单词个数是一(能唯一标志)结束 #include <map> #include ...
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15574 Accepted: 671 ...
- poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
随机推荐
- Java开发程序,使用编辑器编写创建Java项目、类
打开Eclipse 出现界面 工作空间的路径可以选择一个大空间的磁盘存放,点击确定: 创建:程序左上角-文件-新建-JAVA项目 输入项目名(项目名不能为中文),点击完成: 在 包资源管理器中点击 ...
- Oracle 数据库基础学习 (七) SQL语句综合练习
一.多表查询综合练习 1. 列出高于在30部门工作的所有人员的薪金的员工的姓名.部门名称.部门编号.部门人数 分析: 需要的员工信息: |-emp表:姓名.部门编号 |-dept表:部门名称.部门编 ...
- Java04
0.面向对象(Object Oriented:OO): 0.OOA(面向对象分析) OOD(面向对象的设计) OOP(面向对象编程) 1.是软件开发方法 2.扩展的领域:数 ...
- jquery弹出下拉列表插件(实现kindeditor的@功能)
这几天有个工作需求,就是在富文本输入区域(kindeditor)可以有@功能,能够容易提示用户名的(像在qq群组@人一样).在网上找了一个叫bootstrap-suggest的插件,却不能满足我的需求 ...
- hibernate(3) —— 关系映射
hibernate中关系映射指的是实体类与实体类间的关系.和数据库中表与表之间的关系类似,有一对一,多对一,一对多,多对多四种映射关系. 一:一对一映射 两个对象之间是一对一的关系,如人和身份证之间是 ...
- View and Data API 现在支持IE11了
By Daniel Du After a long time waiting, IE11 finally supports WebGL, which enables us viewing our 3D ...
- Android高效计算——RenderScript(一)
高效计算——RenderScript RenderScript是安卓平台上很受谷歌推荐的一个高效计算平台,它能够自动把计算任务分配到各个可用的计算核心上,包括CPU,GPU以及DSP等,提供十分高效的 ...
- @synchronized(self)的用法 小结
@synchronized() 的作用是创建一个互斥锁,保证在同一时间内没有其它线程对self对象进行修改,起到线程的保护作用, 一般在公用变量的时候使用,如单例模式或者操作类的static变量中使用 ...
- Git 简易手册
前言 这里就不介绍 git 了,表一看到命令行就怕怕,常用的命令也就这些,基本满足 99% 以上的项目需求,非常实用. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnbl ...
- Appfuse:起步
在众多开源的Java开源CMS中探索了很久,终于选定了appfuse,理由如下: 1. 简洁:只搭建了框架,没有做多余的事 2. 完成了基本的用户管理:用户.角色.权限的定义很清晰 3. 符合预期的架 ...