POJ 3630Phone List

题目连接:http://poj.org/problem?id=3630

题意:问是否有号码是其他号码的前缀。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
const int N=1e5+;
struct Tire
{
int T[N][];
int sum[N];
int cou;
void init()
{
cou=;
memset(T,,sizeof(T));
memset(sum,,sizeof(sum));
}
void Insert(char *s)
{
int h=,i,n=strlen(s);
for(i=; i<n; i++)
{
if(T[h][s[i]-'']==)
T[h][s[i]-'']=cou++;
h=T[h][s[i]-''];
}
sum[h]++;
}
int ask(char *s)
{
int h=,i=,n=strlen(s);
for(i=; i<n-; i++)
{
if(T[h][s[i]-''])
{
h=T[h][s[i]-''];
if(sum[h]>) return ;
}
else return ;
}
return ;
}
} tire;
char s[][];
int main()
{
int i,T,n;
scanf("%d",&T);
while(T--)
{
int ans=;
scanf("%d",&n);
getchar();
tire.init();
for(i=; i<n; i++)
{
scanf("%s",s[i]);
getchar();
tire.Insert(s[i]);
}
for(i=; i<n; i++)
if(tire.ask(s[i])) ans=;
if(ans==) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return ;
}

POJ 3630

HDU 1251统计难题

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1251

题意:统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=5e5+;
struct Tire
{
int T[N][],sum[N];
int cou;
void Init()
{
cou=;
memset(T,,sizeof(T));
memset(sum,,sizeof(sum));
}
void Insert(char *s)
{
int i,h=,n=strlen(s);
for(i=; i<n; i++)
{
if(T[h][s[i]-'a']==)
T[h][s[i]-'a']=cou++;
h=T[h][s[i]-'a'];
sum[h]++;
}
}
int ask(char *s)
{
int i,h=,n=strlen(s);
for(i=; i<n; i++)
{
if(T[h][s[i]-'a']!=) h=T[h][s[i]-'a'];
else return ;
}
return sum[h];
}
} tire;
int main()
{
char s[];
tire.Init();
while(gets(s))
{
if(strlen(s)==) break;
tire.Insert(s);
}
while(scanf("%s",s)!=EOF)
{
cout<<tire.ask(s)<<endl;
}
return ;
}

HDU 1251

HDU 1004Let the Balloon Rise

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1004

题意:输出颜色数量最多的一种颜色。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=+;
struct Tire
{
int T[N][],sum[N];
int cou;
void Init()
{
cou=;
memset(T,,sizeof(T));
memset(sum,,sizeof(sum));
}
int Insert(char *s)
{
int i,h=,n=strlen(s);
for(i=; i<n; i++)
{
if(T[h][s[i]-'a']==)
T[h][s[i]-'a']=cou++;
h=T[h][s[i]-'a'];
}
sum[h]++;
return sum[h];
}
} tire;
int main()
{
int n;
char s[],ans[];
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
tire.Init();
int Max=;
while(n--)
{
getchar();
scanf("%s",s);
int x=tire.Insert(s);
if(x>Max)
{
Max=x;
strcpy(ans,s);
}
}
printf("%s\n",ans);
}
return ;
}

HDU 1004

HDU 4825Xor Sum

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4825

题意:在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大。

思路:尽量使得K的高位与S的高位不同。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=3e6+;
struct Tire
{
int T[N][],sum[N];
int cou;
void Init()
{
cou=;
memset(T,,sizeof(T));
memset(sum,,sizeof(sum));
}
void Insert(int num)
{
int i=,h=;
for(i=; i>=; i--)
{
if(T[h][(num>>i)&]==)
T[h][(num>>i)&]=cou++;
h=T[h][(num>>i)&];
}
sum[h]=num;
}
int ask(int num)
{
int i=,h=;
for(i=; i>=; i--)
{
int x=(num>>i)&,sign=;
if(x==) sign=;
if(T[h][sign]) h=T[h][sign];
else h=T[h][x];
}
return sum[h];
}
} tire;
int main()
{
int n,m;
int t,T;
scanf("%d",&T);
for(t=; t<=T; t++)
{
tire.Init();
scanf("%d%d",&n,&m);
int num;
while(n--)
{
scanf("%d",&num);
tire.Insert(num);
}
printf("Case #%d:\n",t);
while(m--)
{
scanf("%d",&num);
cout<<tire.ask(num)<<endl;
}
}
return ;
}

HDU 4825

Tire树入门专题的更多相关文章

  1. tire 树入门

    博客: 模板: 前缀是否出现: /* trie tree的储存方式:将字母储存在边上,边的节点连接与它相连的字母 trie[rt][x]=tot:rt是上个节点编号,x是字母,tot是下个节点编号 * ...

  2. Trie树入门

    Trie树入门 貌似很多人会认为\(Trie\)是字符串类型,但是这是数据结构!!!. 详情见度娘 下面开始进入正题. PS:本文章所有代码未经编译,有错误还请大家指出. 引入 先来看一个问题 ​ 给 ...

  3. 主席树入门(区间第k大)

    主席树入门 时隔5个月,我又来填主席树的坑了,现在才发现学算法真的要懂了之后,再自己调试,慢慢写出来,如果不懂,就只会按照代码敲,是不会有任何提升的,都不如不照着敲. 所以搞算法一定要弄清原理,和代码 ...

  4. poj 3841 Double Queue (AVL树入门)

    /****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...

  5. Codeforces 714C. Sonya and Queries Tire树

    C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...

  6. 中文分词系列(二) 基于双数组Tire树的AC自动机

    秉着能偷懒就偷懒的精神,关于AC自动机本来不想看的,但是HanLp的源码中用户自定义词典的识别是用的AC自动机实现的.唉-没办法,还是看看吧 AC自动机理论 Aho Corasick自动机,简称AC自 ...

  7. 中文分词系列(一) 双数组Tire树(DART)详解

    1 双数组Tire树简介 双数组Tire树是Tire树的升级版,Tire取自英文Retrieval中的一部分,即检索树,又称作字典树或者键树.下面简单介绍一下Tire树. 1.1 Tire树 Trie ...

  8. [数据结构]字典树(Tire树)

    概述: Trie是个简单但实用的数据结构,是一种树形结构,是一种哈希树的变种,相邻节点间的边代表一个字符,这样树的每条分支代表一则子串,而树的叶节点则代表完整的字符串.和普通树不同的地方是,相同的字符 ...

  9. UVa 11732 (Tire树) "strcmp()" Anyone?

    这道题也是卡了挺久的. 给出一个字符串比较的算法,有n个字符串两两比较一次,问一共会有多少次比较. 因为节点会很多,所以Tire树采用了左儿子右兄弟的表示法来节省空间. 假设两个不相等的字符串的最长公 ...

随机推荐

  1. Vue2父子组件通信探究

    父组件: <template> <div id="secondcomponent"> <input type="" v-model ...

  2. Openwebrtc

    https://github.com/EricssonResearch ============================= webrtc系列不错的博客 http://blog.csdn.net ...

  3. Mac升级到Yosemite后默认的php版本不支持imagetfftext函数问题解决

    Mac升级到yosemite后,php也自动升级,运行项目的时候发现后台验证码显示不出来.调试一下发现imagetfftext这个函数不存在,应该gd没有安装完全,因为Mac上的php实现系统自带的, ...

  4. 为了解决mysqlbing翻译表字段问题而分析frm文件(持续更新)

    出处:kelvin19840813 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但 ...

  5. 15.8.6 AUTO_INCREMENT Handling in InnoDB

    1 传统模式 innodb_autoinc_lock_mode (“traditional” lock mode) 2 连续模式 innodb_autoinc_lock_mode (“consecut ...

  6. 使用myeclipse为java web项目添加SSH框架

    添加SSH框架时,要严格按照先Struts,再Spring,最后Hibernate.添加方法见下方: 第一步:添加Struts框架 请按照图示一步步认真执行,配置好struts才可以进行下一步 第二步 ...

  7. IT蓝豹--RecyclerView加载不同view实现效果

    本项目由开发者:黄洞洞精心为初学者编辑RecyclerView的使用方法. RecyclerView加载不同view实现效果,支持加载多个view,并且支持用volley获取数据, 项目主要介绍: 初 ...

  8. lua代码设置unity对象的基础属性

    设置对象的父节点: wall.transform:SetParent(GameObject.Find("Walls").transform) 设置颜色: wall:GetCompo ...

  9. linux命令行下的ftp 多文件下载和目录下载

    安装:yum install ftp 使用:ftp + ip (未进入ftp状态下运行) ----------------------------------------- 目标ftp服务器是一个非标 ...

  10. linux组、用户操作相关

    Linux删除用户组和用户时常用的一些命令和参数.1.从组中删除用户编辑/etc/group 找到GROUP1那一行,删除 A或者用命令gpasswd -d A GROUP2.建用户:adduser ...