POJ-2001-Shortest Prefixes
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相关推荐
- OpenJudge/Poj 2001 Shortest Prefixes
1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...
- [poj 2001] Shortest Prefixes (字典树)
Language: Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20503 Accepte ...
- poj 2001 Shortest Prefixes(特里)
主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...
- Poj 2001 Shortest Prefix(字典树模板)
字典树入门题...... 只需在对应结点记录num值,找到num为1时的前缀,是唯一前缀(只是该单词的前缀),或者一直找找到该单词结尾. #include <iostream> #incl ...
- 【POJ - 2001 】Shortest Prefixes (字典树,查询重复前缀区间)
题干: A prefix of a string is a substring starting at the beginning of the given string. The prefixes ...
- Shortest Prefixes
poj2001:http://poj.org/problem?id=2001 题意:给你一些单词,然后让你寻找每个单词的一个前缀,这个前缀能够唯一表示这个单词,并且是最短的. 题解:直接用trie树来 ...
- Shortest Prefixes(字典树)
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- poj 2001 trie
第一道trie 还需要写题来建立自己的代码习惯. 1 #include <cstdio> 2 #include <vector> 3 #include <algorith ...
- java mysql 占位符_在Java中编写带占位符的SQL语句
C#中SQL中带占位符的语句 假设有一张学员信息表Student,通过表中的ID来找学员,查询的SQL语句如下 string sql = string.Format("select * fr ...
- java 获取每月每一天_java 求取某一段时间内的每一天、每一月、每一年
1.求取某一段时间内的每一天 Date date0 = new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-01" ...
最新文章
- 苹果手机怎么设置时间24小时制_外媒实测苹果iPhone 12续航:5G网络下表现不佳...
- Python3学习笔记-字符串和编码
- 高速串行总线设计基础(五)揭秘SERDES高速面纱之多相数据提取电路与线路编码方案
- python连接redis有中文_Python连接Redis并操作
- Uva12716 素数筛思想的应用
- 测试Windows Live Writer对cnBlog的支持
- 安卓相机 高帧率_Android MediaCodec和摄像头:如何实现更高的帧速率从相机获取帧原始数据?...
- Careercup - Microsoft面试题 - 5672369481842688
- linux ftp 没反应,【已回应】FTP无法使用怎么办
- jquery mobile pc上可以滚动 手机上不能上下滚动_胶天下 | 胶粘剂在手机镜片粘接中的应用...
- Boot2Docker 安装运行出现客户端与服务端版本不一致的解决办法
- vi打开GBK编码文件乱码问题
- 火车售票系统(设计模式分析)
- 王者战力接口(免费),附接口文档+小程序源码
- elementui进度条如何设置_Progress 进度条
- 京东开源组件库NutUI 3.1 正式发布:开启多端开发之路
- 捋一捋DDR内存相关的各种频率
- 计算机科学与工程版面费,《计算机工程与设计》版面费问题 - 论文投稿 - 小木虫 - 学术 科研 互动社区...
- 直播回顾|关联网络如何反团伙欺诈——标准答案版
- pd.DataFrame系列