Tire树入门专题
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树入门专题的更多相关文章
- tire 树入门
博客: 模板: 前缀是否出现: /* trie tree的储存方式:将字母储存在边上,边的节点连接与它相连的字母 trie[rt][x]=tot:rt是上个节点编号,x是字母,tot是下个节点编号 * ...
- Trie树入门
Trie树入门 貌似很多人会认为\(Trie\)是字符串类型,但是这是数据结构!!!. 详情见度娘 下面开始进入正题. PS:本文章所有代码未经编译,有错误还请大家指出. 引入 先来看一个问题 给 ...
- 主席树入门(区间第k大)
主席树入门 时隔5个月,我又来填主席树的坑了,现在才发现学算法真的要懂了之后,再自己调试,慢慢写出来,如果不懂,就只会按照代码敲,是不会有任何提升的,都不如不照着敲. 所以搞算法一定要弄清原理,和代码 ...
- poj 3841 Double Queue (AVL树入门)
/****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...
- Codeforces 714C. Sonya and Queries Tire树
C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...
- 中文分词系列(二) 基于双数组Tire树的AC自动机
秉着能偷懒就偷懒的精神,关于AC自动机本来不想看的,但是HanLp的源码中用户自定义词典的识别是用的AC自动机实现的.唉-没办法,还是看看吧 AC自动机理论 Aho Corasick自动机,简称AC自 ...
- 中文分词系列(一) 双数组Tire树(DART)详解
1 双数组Tire树简介 双数组Tire树是Tire树的升级版,Tire取自英文Retrieval中的一部分,即检索树,又称作字典树或者键树.下面简单介绍一下Tire树. 1.1 Tire树 Trie ...
- [数据结构]字典树(Tire树)
概述: Trie是个简单但实用的数据结构,是一种树形结构,是一种哈希树的变种,相邻节点间的边代表一个字符,这样树的每条分支代表一则子串,而树的叶节点则代表完整的字符串.和普通树不同的地方是,相同的字符 ...
- UVa 11732 (Tire树) "strcmp()" Anyone?
这道题也是卡了挺久的. 给出一个字符串比较的算法,有n个字符串两两比较一次,问一共会有多少次比较. 因为节点会很多,所以Tire树采用了左儿子右兄弟的表示法来节省空间. 假设两个不相等的字符串的最长公 ...
随机推荐
- Vuejs使用笔记 --- 框架
这两天学了vuejs的模板,于此纪录一下. 这是整个大文件夹的配置,现在我们看到的所有文件都不需要去管,说要关注也只需要关注“index.html” "index.html"里面是 ...
- ZYNQ fsbl阶段的调试方法
以下是从安富利工程师的技术支持的邮件中摘抄的,在此再次对他们表示感谢. 在我们面对客户单板的时候,fsbl阶段的调试多少会有些问题,在这个过程中怎么快速定位客户的问题,并将有效的信息反馈给希望能帮助到 ...
- FireDac 的RecordCount 相关测试 记录。
unit Unit4; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- delphi 类方法、类变量、类常量、类属性的研究,自己的研究
群里我师傅给我的答案: unit Unit4; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Vari ...
- css 去除点击之后的虚线
链接在被点击时会出现虚线框,即使松开了也仍然存在,在有的时候显得不美观.既然不好看,那就不要它.怎样去掉呢? 方法一 IE下可使用其私有的html属性:hideFoucs,在标签的结构中加入hidef ...
- 微软ASP.NET技术“乱谈”
微软ASP.NET技术“乱谈” 2014新年了,顺手写的一点文字,主要谈谈我对当前微软ASP.NET技术的看法,比较随意,大伙儿随便看看吧. 1 当前微软Web平台技术全貌 从2002年发布.NET ...
- php查看网页源代码的方法
这篇文章主要介绍了php查看网页源代码的方法,涉及php读取网页文件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下 本文实例讲述了php查看网页源代码的方法.分享给大家供大家参考.具体实现 ...
- 关于rank、dense_rank、ROW_NUMBER及OVER(PARTITION BY)、OVER(ORDER BY)的一些用法
CREATE TABLE t_harry ( id int NOT NULL, ) DEFAULT NULL, ChannelID ) DEFAULT NULL, TimeStamp datetime ...
- mysql5.8安装指南
一.安装mysql yum源 从官网http://dev.mysql.com/downloads/repo/yum/下载mysql最新的yum源的rpm安装包 wget http://repo.mys ...
- [原创.数据可视化系列之三]使用Ol3加载大量点数据
不管是百度地图还是高德地图,都很难得见到在地图上加载大量点要素,比如同屏1000的,因为这样客户端性能会很低,尤其是IE系列的浏览器,简直是卡的要死.但有的时候,还真的需要,比如,我要加载全球的AQI ...