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 ...
 
随机推荐
- What does "size" in int(size) of MySQL mean?
			
What does "size" in int(size) of MySQL mean? https://alexander.kirk.at/2007/08/24/what-doe ...
 - mybatis中的查询缓存
			
一: 查询缓存 Mybatis提供查询缓存,用于减轻数据压力,提高数据库压力. Mybatis提供一级缓存和二级缓存. 在操作数据库时需要构造SqlSession对象,在对象中有一个数据结构(Hash ...
 - 服务器判断http是否中断
			
一般来说,只需要send或者recv一下,如果结果为零,则为掉线. 1.当recv返回值为0是表示对方已经正常(MSDN说是完美的)的断开连接.2.当recv返回SOCKET_ERROR时,说明对方已 ...
 - VS 快捷键
			
项目相关的快捷键 Ctrl + Shift + B = 生成项目 Ctrl + Alt + L = 显示Solution Explorer(解决方案资源管理器) Shift + Alt+ C = 添加 ...
 - Vue.js 递归组件实现树形菜单
			
最近看了 Vue.js 的递归组件,实现了一个最基本的树形菜单. 项目结构: main.js 作为入口,很简单: import Vue from 'vue' Vue.config.debug = tr ...
 - IE6/IE7中li底部4px空隙的Bug
			
当li的子元素中有浮动(float)时,IE6/IE7中<li>元素的下面会产生4px空隙的bug. 代码如下: <ul class="list"> < ...
 - js中cookie的添加,删除,查询总结
			
function addCookie(objName,objValue,objHours){//添加cookie var str = objName + "=" + escape( ...
 - Eclipse部署项目的原理简介eclipse,wtpwebapps,tomcat
			
转载请注明出处! http://www.cnblogs.com/libingbin/ 感谢您的阅读.如果文章对您有用,那么请轻轻点个赞,以资鼓励.
 - 常用API——日期型函数Date
			
上图 ·声明 var myDate = new Date(); //系统当前时间 var myDate = new Date(yyyy, mm, dd, hh, mm, ss); var myDate ...
 - 敏捷开发XP
			
一.组建XP团队 在XP团队中,由以下组成 二.项目相关环境 1.利益相关者:与PM一样,对项目进行管理 2.执行发起人:最终客户(必须定期演示) 三.XP组成 四.思考 1.结 ...