#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. System V共享内存区

    要点 shell查看命令:ipcs -m 主要函数 #include <sys/shm.h> //oflag=IPC_CREAT|IPC_EXCL|0644组合 //创建一个内存共享区 i ...

  2. .NET开源工作流RoadFlow-流程设计-流转条件设置(路由)

    当一个步骤后面有多个步骤时,可以设置为根据设置条件系统自动判断该流向哪些步骤,也叫路由. roadflow没有单独的路由步骤来设置条件,流程条件通过双击连线弹出条件设置框来设置. 1.sql条件 即通 ...

  3. VMware-workstation-full-9.0.0-812388+汉化补丁+有效密钥

    更新内容:https://www.vmware.com/support/ws90/doc/workstation-90-release-notes.html 官网下载地址:https://my.vmw ...

  4. Javascript中“==”与“===”的区别

    在Javascript中有"=="和"==="两种比较运行符,那么他们有什么区别呢? 一.对于string,number等基础类型,==和===是有区别的 1) ...

  5. hdu 1509 Windows Message Queue

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1509 Windows Message Queue Description Message queue ...

  6. 2.Modelsim打开时出现的Error

    Modelsim之error “unable to check out a viewer license necessary for use of the modelsim graph.Vsim is ...

  7. 理解JavaScript中的事件路由冒泡过程及委托代理机制

    当我用纯CSS实现这个以后.我开始用JavaScript和样式类来完善功能. 然后,我有一些想法,我想使用Delegated Events (事件委托)但是我不想有任何依赖,插入任何库,包括jQuer ...

  8. oracle增加表空间的四种方法,查询表空间使用情况

    增加表空间大小的四种方法Meathod1:给表空间增加数据文件ALTER TABLESPACE app_data ADD DATAFILE'D:\ORACLE\PRODUCT\10.2.0\ORADA ...

  9. VS中引用第三方库的方法(配置sqlite数据库)

    我们在编写程序时,,不可避免的会使用第三方的库文件,很少使用源文件(.cpp),大部分是使用对类进行声明的头文件和封装了类的链接库(静态lib或动态dll),比如我们写程序用的iostream这个库, ...

  10. gvim 常用配置

    set nocompatible set nu! filetype on syntax on set autoindent set ai! "set bg=dark set autoinde ...