#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. [转]Oracle 10g及pro*c相关问题及解决方法(转)

    Oracle 10g及pro*c相关问题及解决方法 2008年08月21日 星期四 上午 11:21 最近一直在进行ORACLE 10g和PRO*C的学习. 其中遇到了不少的问题: 现列于此,已备他用 ...

  2. IIS 配置错误解决方法集合

    问题:405 - 不允许用于访问此页的 HTTP 谓词 解决:IIS处理程序映射中添加模块映射,模块选择:ServerSideIncludeModule,名称:SSINC-HTML

  3. db2建立表空间

    --缓冲池 CREATE BUFFERPOOL EMP_BUF_POOL  IMMEDIATE  ALL DBPARTITIONNUMS  SIZE 250  NUMBLOCKPAGES 0  PAG ...

  4. Effiective C++ (一)

    最近在看Effective  C++ ,同时将总结一下里边的重要知识点: ##########################    module  1    #################### ...

  5. Oracle HS (Heterogeneous Services)深入解析 及协同Gateway工作流程(转)

    异构的数据源同Oracle Database做交互原理. 图1 上图是一张Oracle 异构连接处理的架构图,其中我们可以看到主要的非数据源模块包括有HS(Heterogeneous Service) ...

  6. oracle12c不能进入到http://localhost:5500/em的解决办法

    Oracle11g企业管理器无法打开——解决https://localhost:1158/em 页面无法打开的问题 常见的问题:https://localhost:1158/em 无法打开 解决办法: ...

  7. EF - 批量插入

    比较一下下面两种方式的区别 1,每Add一次 就savechange() static void Main(string[] args) { //List<User> users= Fin ...

  8. Oracle无法drop用户,提示有连接不能删除时

    百度了一下,这个可以行得通 select username,sid,serial# from v$session alter system kill SESSION '133,169' ; drop ...

  9. Oracle把两个空格以上的空格,替换为两个空格

    substr( ,instr(,)),)) ) 解释如下: 1. 去掉原字串左右的空格的字符(STR),2.查找STR中空格出现二次的位置(LOC),3.从STR中的第一位到LOC-1截取STR||L ...

  10. java提供了native2ascii工具

    可以使用这个工具,把中文编码称为ascii码 在命令行输入native2ascii 输入中文 得到数据