[openjudge] 2797最短前缀 Trie
描述
一个字符串的前缀是从该字符串的第一个字符起始的一个子串。例如 "carbon"的字串是: "c", "ca", "car", "carb", "carbo", 和 "carbon"。注意到这里我们不认为空串是字串, 但是每个非空串是它自身的字串. 我们现在希望能用前缀来缩略的表示单词。例如, "carbohydrate" 通常用"carb"来缩略表示. 现在给你一组单词, 要求你找到唯一标识每个单词的最短前缀
在下面的例子中,"carbohydrate" 能被缩略成"carboh", 但是不能被缩略成"carbo" (或其余更短的前缀) 因为已经有一个单词用"carbo"开始
一个精确匹配会覆盖一个前缀匹配,例如,前缀"car"精确匹配单词"car". 因此 "car" 是 "car"的缩略语是没有二义性的 , “car”不会被当成"carriage"或者任何在列表中以"car"开始的单词.
输入输入包括至少2行,至多1000行. 每行包括一个以小写字母组成的单词,单词长度至少是1,至多是20.输出输出的行数与输入的行数相同。每行输出由相应行输入的单词开始,后面跟着一个空格接下来是相应单词的没有二义性的最短前缀标识符。
样例输入
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate
样例输出
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona 暴力的做法 :遍历每个单词可能的前缀 ,并在其他单词从搜索, 如果包含在其他某个单词的前面, 就不能作为前缀 ,搜寻下一个可能的前缀。
如果所有可能的前缀都在其他某个含有就是其本身。
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
using namespace std; char w[][];
char pre[];
int n; bool in(char *s, int index)
{
for (int i = ; i < n; i++) {
if (strcmp(s, w[index]) == )
return ;
if (i == index)
continue;
if (strstr(w[i], s)== &w[i][])
return ;
}
return ;
} int main()
{
//freopen("1.txt", "r", stdin);
n = ;
while (~scanf("%s", w[n++])); for (int i = ; i < n; i++) {
for (int j = ; j < strlen(w[i]); j++) {
memset(pre, , sizeof(pre));
strncpy(pre, w[i], j+);
if (!in(pre, i)) {
strcat(w[i], " ");
strcat(w[i], pre);
break;
}
}
} for (int i = ; i < n; i++)
puts(w[i]); return ;
}
也可利用Trie,字典树
先建树,一直查找字符串的前缀,一直到某个字母,num为1,即以此为前缀的字符串为1,是唯一的
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std; struct Trie
{
Trie *next[];
int num;
Trie()
{
for (int i = ; i < ; i++)
next[i] = NULL;
num = ;
}
}; Trie root;
void Insert(char *s)
{
Trie *p = &root;
for (int i = ; s[i]; i++) {
int t = s[i]-'a';
if (p->next[t] == NULL)
p->next[t] = new Trie;
p = p->next[t];
p->num++;
}
} void Find(char *s)
{
Trie *p = &root;
for (int i = ; s[i]; i++) {
int t = s[i]-'a';
if (p->next[t] == NULL)
return;
p = p->next[t];
printf("%c", s[i]);
if (p->num == )
return;
}
} int main()
{
//freopen("1.txt", "r", stdin);
int n = ;
char w[][];
while (~scanf("%s", w[n])) {
Insert(w[n]);
n++;
} for (int i = ; i < n; i++) {
printf("%s ", w[i]);
Find(w[i]);
printf("\n");
} return ;
}
[openjudge] 2797最短前缀 Trie的更多相关文章
- 【OpenJ_Bailian - 2797】最短前缀(贪心)
最短前缀 Descriptions: 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的字串是: "c", "ca&qu ...
- NOI题库1799 最短前缀
1799:最短前缀 总时间限制: 1000ms 内存限制: 65536kB 描述 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的字串是: &quo ...
- 【openjudge】【前缀和】P6731啤酒厂选址
[描述] 海上有一个岛,在环海边上建有一条环岛高速公路,沿着公路有n(5 < n < 10000)个居民点,假设每个居民点有一个编号,从0开始,按顺时针依次从小到大(即,0,1,…,n-1 ...
- 字典树---2001 POJ Shortest Prefixes(找最短前缀)
做的第一道字典树的题,算比较水的: -->>>:传送门 代码: #include <stdio.h> #include<stdlib.h> #define M ...
- TRIE 字典树 前缀紧急集合!
TRIE: 在计算机科学中,Trie,又称前缀树或字典树,是一种有序树状的数据结构,用于保存关联数组,其中的键通常是字符串.——百度百科 自我理解: trie树,是一种处理字符串前缀的数据结构,通常会 ...
- Trie(前缀树/字典树)及其应用
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
- 第一课trie 树 POJ 2001
最短前缀(Openjudge上抄的) 总时间限制: 1000ms 内存限制: 65536kB 描述 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的 ...
- POJ 2001-Shortest Prefixes(Trie 入门)
题意:给你一组串,求每个串在组中唯一标志的最短前缀 分析:保存树上经过各节点的单词个数,扫描每个串当经过节点单词个数是一(能唯一标志)结束 #include <map> #include ...
- poj 2001 Shortest Prefixes trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
随机推荐
- sed 和awk结合取ip 地址
1.打印文件的第一列(域) : awk '{print $1}' filename 2.打印文件的前两列(域) : awk '{prin ...
- 深入理解Java虚拟机到底是什么(转)
原文链接:http://blog.csdn.net/zhangjg_blog/article/details/20380971 什么是Java虚拟机 作为一个Java程序员,我们每天都在写Java ...
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...
- Android 虚拟机 程序安装目录
Android应用安装涉及到如下几个目录:system/app系统自带的应用程序,无法删除.data/app用户程序安装的目录,有删除权限.安装时把apk文件复制到此目录.data/data存放应用程 ...
- JavaScript多态
function Master(){ //给动物喂食 this.feed=function(animal,food){ window.alert(animal.constructor); docume ...
- 程序猿老公去米国参加 WWDC,顺便想带渡老婆蜜月,如何办签证?
这个问题要拆开描述比较好:1. 老公是苹果开发者,抽中了2014 WWDC购票机会,打算自费去参加.如果自己成行,应该办何种签证?2. 顺带,两人新婚半年还未安排蜜月,打算提前几天过去先游览一下西海岸 ...
- listen 69
Today Is Unlucky for People Who Have Bad Luck Today If you have Paraskevidekatriaphobia, today is no ...
- 【Shell】Linux 一行 多命令
http://www.cnblogs.com/koreaseal/archive/2012/05/28/2522178.html 要实现在一行执行多条Linux命令,分三种情况: 1.&&am ...
- 「NOIP2017」「LuoguP3952」 时间复杂度(模拟,栈
题目描述 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机会来啦!下面请你编写程序 ...
- 【Lintcode】136.Palindrome Partitioning
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...