hdu3724Encoded Barcodes(Trie tree)
题目大意:给n个字符串,给m个询问,每个询问给k个条形码。每个条形码由8个小码组成,每个小码有相应的宽度,已知一个条形码的宽度只有2种,宽的表示1,窄的表示0。并且宽的宽度是窄的宽度的2倍。由于扫描的时候有误差,每个小码的宽度为一个浮点型数据,保证每个数据的误差在5%内。所以一个条形码可以对应一个ASCC码,表示一个小写字母。k个条形码表示一个字符串s,每个询问表示给定的m个字符串中以s为前缀的字符串个数。
题目分析:将n个字符串插入到字典树中,并记录下每个前缀有多少个字符串。即每插入一个字符串,在相应路径上+1,然后就是模拟出字符串s,在字典树中查询即可。
关于条形码数据的处理:输入的时候记录下8个数据中的最大值,然后对于所有的数据,满足fabs(mx - data[i])/mx < 0.1的,那么第i为为1,否则0。
详情请见代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
using namespace std;
const int N = 10005;
const int M = 2005;
const int NM = 10000005;
const double eps = 1e-6;
typedef __int64 ll; struct node
{
int cnt;
int next[26];
}trie[NM];
int num,n,m;
double dt[9];
char str[M];
void init(int x)
{
memset(trie[x].next,0,sizeof(trie[x].next));
trie[x].cnt = 0;
}
void insert(int cur,int dp,int len)
{
trie[cur].cnt ++;
if(dp == len)
return;
if(trie[cur].next[str[dp] - 'a'] == 0)
{
trie[cur].next[str[dp] - 'a'] = num;
init(num);
num ++;
}
insert(trie[cur].next[str[dp] - 'a'],dp + 1,len);
}
int query(int cur,int dp,int len)
{
if(dp == len)
return trie[cur].cnt;
if(trie[cur].next[str[dp] - 'a'])
return query(trie[cur].next[str[dp] - 'a'],dp + 1,len);
else
return 0;
}
int main()
{
int i,j,k;
while(scanf("%d%d",&n,&m) != EOF)
{
num = 1;
ll ans = 0;
init(0);
while(n --)
{
scanf("%s",str);
int len = strlen(str);
insert(0,0,len);
}
while(m --)
{
scanf("%d",&k);
for(i = 0;i < k ;i ++)
{
double mx = 0;
int code = 0;
for(j = 0;j < 8;j ++)
{
scanf("%lf",&dt[j]);
if(dt[j] - mx > eps)
mx = dt[j];
}
for(j = 0;j < 8;j ++)
{
if(fabs(mx - dt[j])/mx - 0.1 < eps)
code += (1<<(7 - j));
}
str[i] = code;
}
str[k] = '\0';
ans += query(0,0,k);
}
printf("%I64d\n",ans);
}
return 0;
}
hdu3724Encoded Barcodes(Trie tree)的更多相关文章
- 关于Trie Tree简单实现
最近突然有兴致hiho一下了,实现了下trie tree,感觉而言,还是挺有意思的,个人觉得这货不光可以用来查单词吧,其实也可以用来替代Hash,反正查找,插入复杂度都挺低的,哈哈,啥都不懂,瞎扯.. ...
- 字典树(Trie Tree)
终于要开始更新我的ACM学习之路了,不过没想到却是因为一次Java大作业,有趣,%yuan老师. 字典树是一种很简单的树形结构,主要用来进行词频统计,在算法竞赛中有时也会碰到. 字典树的基本思路是,通 ...
- 笔试算法题(39):Trie树(Trie Tree or Prefix Tree)
议题:TRIE树 (Trie Tree or Prefix Tree): 分析: 又称字典树或者前缀树,一种用于快速检索的多叉树结构:英文字母的Trie树为26叉树,数字的Trie树为10叉树:All ...
- Phone List POJ 3630 Trie Tree 字典树
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29416 Accepted: 8774 Descr ...
- Trie tree实践
1.Trie树 Trie树即字典树或前缀树, 2.实践 代码实践如下: package cn.edu.buaa.trie; import java.util.HashSet; /** * @autho ...
- 字典树(Trie Tree)
在图示中,键标注在节点中,值标注在节点之下.每一个完整的英文单词对应一个特定的整数.Trie 可以看作是一个确定有限状态自动机,尽管边上的符号一般是隐含在分支的顺序中的.键不需要被显式地保存在节点中. ...
- trie tree(字典树)
hihocoder题目(http://hihocoder.com/problemset):#1014 trie树 #include <iostream> using namespace s ...
- HDU 3724 Encoded Barcodes (Trie)
题意:给n个字符串,给m个询问,每个询问给k个条形码.每个条形码由8个小码组成,每个小码有相应的宽度,已知一个条形码的宽度只有2种,宽的表示1,窄的表示0.并且宽的宽度是窄的宽度的2倍.由于扫描的时候 ...
- Find the Clones Trie Tree
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8306 Accepted: 3130 Description Doubl ...
随机推荐
- Android模拟器访问本地的localhost失败及解决方案
在开发手机网站是,使用Android模拟器测试,在手机浏览器中输入localhost访问本地服务器失败! 原因: 在Android系统中localhost就是127.0.0.1 在Windows系统中 ...
- c - 根据首字母判断星期几
#include <stdio.h> #include <ctype.h> /* 请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母. */ ...
- [转帖]了解AmbiLight知识
了解科技前沿的方法..American Online=AOL.algorithm算法.(Denzel say, and I don't know) Engadget瘾科技网站.英文版Engadget网 ...
- 【转】 UITableView 的indexPath
原文:http://blog.csdn.net/mengtnt/article/details/6733691 前面说过了viewController的一些基本注意事项.这里针对不同的viewCont ...
- poj3278 BFS入门
M - 搜索 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:65536KB 64bit I ...
- STL中的set容器
#include <iostream> #include <set> using namespace std; int main() { set<int> s; s ...
- Selenium 下载URL
http://mvnrepository.com/artifact/org.seleniumhq.selenium
- oracle通过query导出指定条件的数据
通过下面的方式oracle可以导出指定了条件的数据: exp mixcoaldb/mixcoaldb@server tables=(shengcssjk) query=\"where to_ ...
- 解决SQL Server Management Studio Express不支持更新全文目录的方法
微软的说法:https://msdn.microsoft.com/zh-cn/library/ms365247.aspx 可以用命令创建: A.创建唯一索引.全文目录和全文索引 以下示例对 Adven ...
- Powerpoin怎么制作电子相册|PPT制作电子相册教程
Powerpoin怎么制作电子相册?你是不是也对这一问题颇感兴趣呢?下面小编就为大家带来PPT制作电子相册详细教程,赶紧准备好你的自拍照什么的,开启Powerpoin制作电子相册之旅吧! Powerp ...