POJ 2001
#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的更多相关文章
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- POJ 2001 Shortest Prefixes (Trie)
题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...
- 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(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
- 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 Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- POJ 2001 Shortest Prefix
字典树基本题. 代码: #include <iostream> #include <cstdio> #include <cstring> #include < ...
- POJ 2001:Shortest Prefixes
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16782 Accepted: 728 ...
- POJ 2001 字典树(入门题)
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #i ...
随机推荐
- spring中scope(作用越)理解
今天总结了一下spring中作用域scope的用法.在spring中作用域通过配置文件形式的用法如下. <bean id="role" class="spring. ...
- SQL Server 批量插入数据的两种方法
在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍 SQL Server支持的两种批 ...
- Python修饰器的函数式编程
Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...
- Xcode全局断点
1.将导航器视图切换到断点导航器视图下,也可以用快捷键Command+7一步搞定,键盘是window风格的用户Command键是win键(有微软logo),然后点击左下角的+号,选择Add Symbo ...
- ios8中的UIScreen
let orientation: UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation pri ...
- Entity Framework 学习第一天
文章是作为初学者记录之用,没有学习过的同学可以借鉴一下,至于用过和高手嘛,就算了吧.仅是入门.废话不多说了,马上新建个项目,添加Entity Framework,这个词以下将用EF代替. 本文使用的I ...
- 【Same Tree】cpp
题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
- 【WildCard Matching】cpp
题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...
- Chapter 3 Discovering Classes and Object
Chatper 3 Discovering Classes and Object Exercises: 1.What is a class? A class is a template for man ...
- Netsharp快速入门(之3) 基础档案(之B 实体建模 新建项目)
作者:秋时 杨昶 时间:2014-02-15 转载须说明出处 3.3 创建业务模型 3.3.1 新建项目 1.打开业务模型 2.打开业务模型后,在工具栏上选择操作-新增项目分类,标识这 ...