传送门

题目大意:求最短唯一前缀

题解:Trie树 把单词一个个插入,每个字母节点v[]++;然后输出时输出到v[]为1的点,

v[]=1说明只有这个单词经过。

代码 :

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100009
using namespace std; int n,cnt; int trie[N*][],v[N]; char s[N][]; void insert(int x)
{
int root=,len=strlen(s[x]);
for(int i=;i<len;i++)
{
int id=s[x][i]-'a';
if(trie[root][id]==) trie[root][id]=++cnt;
root=trie[root][id];
v[root]++;
}
} void find(int x)
{
cout<<s[x]<<" ";
int root=,len=strlen(s[x]);
for(int i=;i<len;i++)
{
int id=s[x][i]-'a';
//cout<<"---"<<trie[root][id]<<endl;
if(v[root]==) break;
cout<<s[x][i];
root=trie[root][id];
}
printf("\n");
} int main()
{
while(scanf("%s",s[++n])!=EOF){insert(n);}
for(int i=;i<=n;i++) find(i);
return ;
}

POJ2001Shortest Prefixes(Trie树)的更多相关文章

  1. POJ2001Shortest Prefixes[Trie]

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17683   Accepted: 768 ...

  2. POJ2001Shortest Prefixes(字典树)

    题目大意就是帮你给N条字符串,每条长度不超过20.问要将他们单一识别出来,每个字符串最短可以缩为多短. 如: abc abcdefg bc adef 这四个字符串就可分别缩写为 abc abcd b ...

  3. poj2001 Shortest Prefixes (trie树)

    Description A prefix of a string is a substring starting at the beginning of the given string. The p ...

  4. POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

  5. POJ 2001 Shortest Prefixes 【Trie树】

    <题目链接> 题目大意: 找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串. 解题分析: Trie树的简单应用,对于每个单词的插入,都在相应字符对应的节点 num 值+1 , ...

  6. POJ2001 Shortest Prefixes (Trie树)

    直接用Trie树即可. 每个节点统计经过该点的单词数,遍历时当经过的单词数为1时即为合法的前缀. type arr=record next:array['a'..'z'] of longint; w: ...

  7. Trie 树 及Java实现

    来源于英文“retrieval”.   Trie树就是字符树,其核心思想就是空间换时间. 举个简单的例子.   给你100000个长度不超过10的单词.对于每一个单词,我们要判断他出没出现过,如果出现 ...

  8. poj 2001 Shortest Prefixes trie入门

    Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...

  9. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

随机推荐

  1. 快速幂取模&快速乘取模

    快速幂取模 即快速求出(a^b)mod c 的值.由于当a.b的值非常大时直接求a^b可能造成溢出,并且效率低. 思路 原理就是基于\(a*b \% c = ((a \% c)*(b \% c))\% ...

  2. 2019 DevOps 必备面试题——容器化和虚拟化

    原文地址:https://medium.com/edureka/devops-interview-questions-e91a4e6ecbf3 原文作者:Saurabh Kulshrestha 翻译君 ...

  3. EXCEPTION_ACCESS_VIOLATION(0xc0000005)

    EXCEPTION_ACCESS_VIOLATION(0xc0000005)eclipse.ini中添加:-XX:CompileCommand=exclude,org.eclipse.jdt.inte ...

  4. OS X 下 OpenGL 4.x 环境配置

    配置: OS X 10.10 + CMake 3.2.2 + GLFW 3.1.1 + OpenGL 4.1 + Xcode 6.0 本文主要介绍如何在 OS X 系统下进行环境配置,使得 Xcode ...

  5. combination sum && combination sum II

    1.Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), f ...

  6. tensorflow和pytorch教程

    https://github.com/dragen1860/Deep-Learning-with-TensorFlow-book

  7. linux的装配与虚拟机的快照

    一.科普 1969年,“c语言之父”,“b语言之父”,ken Thompson,开发了一个叫unics系统,是unix系统的雏形,只不过此时的UNICS是用汇编语言写的.移植到其它计算机上需要改很多源 ...

  8. Swoole Redis 连接池的实现

    概述 这是关于 Swoole 入门学习的第九篇文章:Swoole Redis 连接池的实现. 第八篇:Swoole MySQL 连接池的实现 第七篇:Swoole RPC 的实现 第六篇:Swoole ...

  9. Spring Boot可执行Jar包运行原理

    目录 1. 打可执行Jar包 2. 可执行Jar包内部结构 3. JarLauncher 4. 简单总结 5. 远程调试 Spring Boot有一个很方便的功能就是可以将应用打成可执行的Jar.那么 ...

  10. pecl安装和卸载示例

    比如安装MongoDB扩展 命令行下输入: [#] /usr/local/php/bin/pecl install mongodb ......嗒吧嗒吧一大堆输出后 Build process com ...