#include<iostream>
using namespace std;
const int kind=;
struct trienode
{
trienode * next[kind];
int branch;
trienode()
{
branch=;
for(int i=;i<kind;i++)
next[i]=NULL;
}
};
class trie
{ trienode * root;
public:
trie()
{
root=NULL;
}
void insert(char s[])
{
trienode * location = root;
if(location == NULL)
location = root = new trienode();
int i = ,k;
while(s[i])
{
k=s[i]-'a';
if(location->next[k])
location->next[k]->branch++;
else
{
location->next[k]=new trienode();
location->next[k]->branch++;
}
i++;
location = location->next[k];
}
}
int search(char s[]){
trienode * location = root;
if(!location)
return ;
int k,i = ,ans;
while(s[i])
{
k=s[i] - 'a';
if(!location->next[k])
return ;
cout<<s[i];
ans = location->next[k]->branch;
if(ans == )
return ;
location = location->next[k];
i++;
}
return ans;
} };
int main()
{
//freopen("acm.acm","r",stdin);
char a[][];
char b[];
int i = ;
int j;
int num;
trie mytrie;
while(cin>>a[i])
{
mytrie.insert(a[i]);
++ i;
}
for(j = ; j < i; ++ j)
{
cout<<a[j]<<" ";
mytrie.search(a[j]);
cout<<endl;
}
return ;
}

关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。

技术网站地址: vmfor.com

POJ 2001的更多相关文章

  1. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  2. POJ 2001 Shortest Prefixes (Trie)

    题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...

  3. OpenJudge/Poj 2001 Shortest Prefixes

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

  4. poj 2001 Shortest Prefixes(字典树)

    题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...

  5. poj 2001 Shortest Prefixes(特里)

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

  6. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  7. POJ 2001 Shortest Prefix

    字典树基本题. 代码: #include <iostream> #include <cstdio> #include <cstring> #include < ...

  8. POJ 2001:Shortest Prefixes

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16782   Accepted: 728 ...

  9. POJ 2001 字典树(入门题)

    #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #i ...

随机推荐

  1. 对Iframe和图表设置高度的优质代码

    //对Iframe和图表设置高度 function f() { parent.window.setWinHeight(parent.window.document.getElementById(&qu ...

  2. hdu 2112 HDU Today

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的 ...

  3. WIN服务器出现 php-cgi.exe - FastCGI 进程意外退出

    既然是不能解析PHP,那就直接运行一下PHP,看会报什么错,再对症下药,于是,在命令提示符窗口进入php安装的根目录,然后运行php -v的命令,这时窗口弹出计算机丢失msvcr110.dll的错误, ...

  4. 微软云平台媒体服务实践系列 2- 使用动态封装为iOS, Android , Windows 等多平台提供视频点播(VoD)方案

    文章微软云平台媒体服务实践系列 1- 使用静态封装为iOS, Android 设备实现点播(VoD)方案  介绍了如何针对少数iOS, Android 客户端的场景,出于节约成本的目的使用媒体服务的静 ...

  5. [译]rabbitmq 2.5 Where’s my message? Durability and you

    我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. There’s a dirty secret about creating queues and exchanges in ...

  6. Objective - C中属性和点语法的使用

    一.属性        属性是Objective—C 2.0定义的语法,为实例变量提供了setter.getter方法的默认实现能在一定程度上简化程序代码,并且增强实例变量的访问安全性         ...

  7. HTML/CSS的学习过程一览

    HTML/CSS的学习过程一览 说明 调试工具使用的是Google Chrome浏览器,其余浏览器出现的问题,这锅我不背[傲娇脸 可以使用浏览器查看源代码 网页列表 HTML_CSS_1 HTML基本 ...

  8. Asp.Net Web API开发微信后台

    如果说用Asp.Net开发微信后台是非主流,那么Asp.Net Web API的微信后台绝对是不走寻常路. 需要说明的是,本人认为Asp.Net Web API在开发很多不同的请求方法的Restful ...

  9. UML详解

    学习c++必不可少UML,UML从考虑系统的不同角度出发,定义了用例图.类图.对象图.状态图.活动图.序列图.协作图.构件图.部署图等9种图.这些图从不同的侧面对系统进行描述.系统模型将这些不同的侧面 ...

  10. 53张牌中找出缺少的牌的花色和点数--raid3,4,5,6的容错原理

    一副扑克牌,抽出一张,要求找出抽出的牌的点数和花色. 算法的主要思想就是用异或运算来确定丢的牌的花色.四种花色分别如下表示:红桃用1(二进制0001)表示,黑桃用2(二进制0010)表示,黑桃用4(0 ...