POJ-2001-Shortest Prefixes

http://poj.org/problem?id=2001

找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串

用字典树即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
char list[1005][25];
struct node
{int count;node *childs[26];node(){count=0;int i;for(i=0;i<26;i++)childs[i]=NULL;}
};
node *root=new node;
node *current,*newnode;
void insert(char *str)
{int i,m;current=root;for(i=0;i<strlen(str);i++){m=str[i]-'a';if(current->childs[m]!=NULL){current=current->childs[m];++(current->count);}else{newnode=new node;++(newnode->count);current->childs[m]=newnode;current=newnode;}}
}
void search(char *str)
{int i,m;char ans[25];current=root;for(i=0;i<strlen(str);i++){m=str[i]-'a';current=current->childs[m];ans[i]=str[i];ans[i+1]='\0';if(current->count==1)    //可以唯一标示该字符串的前缀{printf("%s %s\n",str,ans);return;}}printf("%s %s\n",str,ans);  // 否则输出该字符串
}
int main()
{int i,t=0;while(scanf("%s",list[t])!=EOF){insert(list[t]);t++;}for(i=0;i<t;i++)search(list[i]);return 0;
}

POJ-2001-Shortest Prefixes相关推荐

  1. OpenJudge/Poj 2001 Shortest Prefixes

    1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...

  2. [poj 2001] Shortest Prefixes (字典树)

    Language: Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20503 Accepte ...

  3. poj 2001 Shortest Prefixes(特里)

    主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...

  4. Poj 2001 Shortest Prefix(字典树模板)

    字典树入门题...... 只需在对应结点记录num值,找到num为1时的前缀,是唯一前缀(只是该单词的前缀),或者一直找找到该单词结尾. #include <iostream> #incl ...

  5. 【POJ - 2001 】Shortest Prefixes (字典树,查询重复前缀区间)

    题干: A prefix of a string is a substring starting at the beginning of the given string. The prefixes ...

  6. Shortest Prefixes

    poj2001:http://poj.org/problem?id=2001 题意:给你一些单词,然后让你寻找每个单词的一个前缀,这个前缀能够唯一表示这个单词,并且是最短的. 题解:直接用trie树来 ...

  7. Shortest Prefixes(字典树)

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

  8. poj 2001 trie

    第一道trie 还需要写题来建立自己的代码习惯. 1 #include <cstdio> 2 #include <vector> 3 #include <algorith ...

  9. java mysql 占位符_在Java中编写带占位符的SQL语句

    C#中SQL中带占位符的语句 假设有一张学员信息表Student,通过表中的ID来找学员,查询的SQL语句如下 string sql = string.Format("select * fr ...

  10. java 获取每月每一天_java 求取某一段时间内的每一天、每一月、每一年

    1.求取某一段时间内的每一天 Date date0 = new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-01" ...

最新文章

  1. 苹果手机怎么设置时间24小时制_外媒实测苹果iPhone 12续航:5G网络下表现不佳...
  2. Python3学习笔记-字符串和编码
  3. 高速串行总线设计基础(五)揭秘SERDES高速面纱之多相数据提取电路与线路编码方案
  4. python连接redis有中文_Python连接Redis并操作
  5. Uva12716 素数筛思想的应用
  6. 测试Windows Live Writer对cnBlog的支持
  7. 安卓相机 高帧率_Android MediaCodec和摄像头:如何实现更高的帧速率从相机获取帧原始数据?...
  8. Careercup - Microsoft面试题 - 5672369481842688
  9. linux ftp 没反应,【已回应】FTP无法使用怎么办
  10. jquery mobile pc上可以滚动 手机上不能上下滚动_胶天下 | 胶粘剂在手机镜片粘接中的应用...
  11. Boot2Docker 安装运行出现客户端与服务端版本不一致的解决办法
  12. vi打开GBK编码文件乱码问题
  13. 火车售票系统(设计模式分析)
  14. 王者战力接口(免费),附接口文档+小程序源码
  15. elementui进度条如何设置_Progress 进度条
  16. 京东开源组件库NutUI 3.1 正式发布:开启多端开发之路
  17. 捋一捋DDR内存相关的各种频率
  18. 计算机科学与工程版面费,《计算机工程与设计》版面费问题 - 论文投稿 - 小木虫 - 学术 科研 互动社区...
  19. 直播回顾|关联网络如何反团伙欺诈——标准答案版
  20. pd.DataFrame系列

热门文章

  1. 个人站长常用的4款网站统计工具
  2. Flex Builder 快捷键
  3. 分析了16年的福利彩票记录,原来可以用Python这么买彩票
  4. HDU4466_Triangle
  5. 目前工资最高的几家外包公司汇总!(2023最新版)
  6. think-cell 加载项在 Office 出现故障后消失丨使用教程
  7. javascript之模拟滚动条
  8. 什么是序列化、反序列化
  9. Unicode算法漏洞几乎祸害所有编程语言,Rust紧急发布补丁
  10. js根据后缀名判断文件的类型