poj2001 Shortest Prefixes(字典树)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 21642 | Accepted: 9269 |
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
- 题意:先输出原来输入的字符串,在输出该字符串在字典树上的共有部分再多一个字符
注意:最后结尾要多输出一个回车
- #include<iostream>
- #include<string>
- #include<string.h>
- using namespace std;
- int tree[][],vis[];
- string s[];
- int id,root,len,num=,t=;
- void insert()
- {
- root=;
- len=s[t].length();
- for(int i=;i<len;i++)
- {
- id=s[t][i]-'a';
- if(!tree[root][id])
- tree[root][id]=++num;
- vis[tree[root][id]]++;
- root=tree[root][id];
- }
- }
- int search(string ss)
- {
- root=;
- len=ss.length();
- for(int i=;i<len;i++)
- {
- id=ss[i]-'a';
- cout<<ss[i];
- if(vis[tree[root][id]]==)
- break;
- root=tree[root][id];
- }
- return ;
- }
- int main()
- {
- while(cin>>s[t])
- {
- insert();
- t++;
- }
- for(int i=;i<t;i++)
- {
- cout<<s[i]<<' ';
- int temp;
- temp=search(s[i]);
- cout<<endl;
- }
- cout<<endl;//多输出一个回车
- return ;
- }
poj2001 Shortest Prefixes(字典树)的更多相关文章
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
- poj2001 Shortest Prefixes (trie树)
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
- POJ2001Shortest Prefixes(字典树)
题目大意就是帮你给N条字符串,每条长度不超过20.问要将他们单一识别出来,每个字符串最短可以缩为多短. 如: abc abcdefg bc adef 这四个字符串就可分别缩写为 abc abcd b ...
- POJ2001 Shortest Prefixes (Trie树)
直接用Trie树即可. 每个节点统计经过该点的单词数,遍历时当经过的单词数为1时即为合法的前缀. type arr=record next:array['a'..'z'] of longint; w: ...
- POJ2001 Shortest Prefixes
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- poj2001 Shortest Prefixes (trie)
读入建立一棵字母树,并且每到一个节点就增加这个节点的覆盖数. 然后再重新扫一遍,一旦碰到某个覆盖数为1就是这个单词的最短前缀了. 不知为何下面的程序一直有bug……不知是读入的问题? type nod ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
随机推荐
- Android WebView 捕捉点击的URL中的信息
项目要求,在WebView中点击搜索关键字,加载其他Web页面时,需要在一个文本输入框中,实时显示关键字 事实上,这种点击,是WebView内的,并没有跳出这个WebView,Activity也没有经 ...
- Android 图片相关
从asset文件夹中读取Bitmap //从asset文件夹中取文件 private Bitmap getImageFromAssetFile(String fileName){ Bitmap ima ...
- structs2----数据封装以及拦截器
技术分析之在Struts2框架中使用Servlet的API 1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据,在Action中使用Servlet ...
- 并发调试和JDK8新特性
- 网络笔记-unity 实现AOP
该文章来自网络,如有冒犯,请及时联系! 前提 引用以下文件 Microsoft.Practices.ObjectBuilder2.dll Microsoft.Practices.Unity.dll M ...
- HDU 2102 A计划 (BFS或DFS)
题意:中文题. 析:是一个简单的搜索,BFS 和 DFS都可行, 主要是这个题有一个坑点,那就是如果有一层是#,另一个层是#或者*,都是过不去的,就可以直接跳过, 剩下的就是一个简单的搜索,只不过是两 ...
- [raspberry pi3] hadoop 编译搭建和配置
Causion: 只有一个raspberry pi3的就随便玩玩吧,瓶颈不在在cpu, 1 G的内存实在是太少了,跑个hadoop就很辛苦了 下面是瞎折腾的过程: oracle的arm jdk的安装过 ...
- 浅聊本人学习React的历程——第一篇生命周期篇
作为一个前端小白,在踏入前端程序猿行业的第三年接触了React,一直对于框架有种恐惧感,可能是对陌生事物的恐惧心里吧,导致自己一直在使用原生JS和JQ作为开发首选,但是在接触了React之后,发现了其 ...
- Java框架之spring 项目 附加之noteresult(status msg data uuid MD5)
1.1创建类 实体类 NoteResult<T> 注意<T> private int status; private String msg; private T dat ...
- 析构函数-复制构造函数-赋值操作符重载-默认构造函数<代码解析>
通过下面primer中的一道习题,可以更深刻的了解,析构函数,复制构造函数,赋值操作符重载,默认构造函数的使用. 但是我的结果与primer习题解答里面的并不相同,可能是编译器不同的原因导致. // ...