UVALive 5029 字典树
Crawling in process...Crawling failedTime Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description

All the big malls need a powerful system for the products retrieval. Now you are employed design a sub-system: reading the barcodes and return the matching products.
A barcode is an optical machine-readable representation of data, which shows certain data on certain products. A barcode consists of a series of bars with different widths. In our system, the barcodes have been scanned and the widths have been recorded. Every consecutive eight bars are considered as representing the ASCII code of a character, each bar for each bit. Ideally, there should be only two kinds of widths in the eight bars, and the width of the wider bar is twice of the narrower. The wider bar indicates 1, while the narrower indicates 0. However, due to the inaccuracy of printing and scanning, there will be an error of at most 5%. That is, if the pretended exact width is x, you may get a value in the range [0.95x, 1.05x].
For example, the width sequence ``10.0 20.0 10.0 10.0 10.0 10.0 10.0 20.0" is a valid barcode of our system, and it means (01000001)2, which is (65)10 and the corresponding character is ``A". Note that ``10.5 20.1 10.1 10.2 9.9 9.7 10.0 19.9" is also a valid barcode representing the same letter.
You are given the names of all the products and many queries. Every name contains lower-case letters only, and the length is no more than 30. The queries are represented as barcodes. For each query, you should decode it to a string S, and report the amount of products whose prefix is S. For the output may be very large, you only need to output the sum of all the queries for each case.
Input
There are several test cases in the input. The first line of each case contains two integers N and M(1N
10000, 1
M
2000), indicating the number of products and queries. Then N lines follow, indicating the names of the products. Note that the names may be duplicated. Then M query blocks follow. The first line of each query block is an integer K(0 < K
30) indicating the length of the query, then K lines follow, each line contains 8 positive float numbers, indicating the barcode for each character.
You can assume that the barcodes are always valid, and always represent lower-case letters.
Output
Output one line for each test case, indicating the sum of all the query results as described above.
Explanation for the sample:
There is only one test case. The first query is ``a", and the answer is 3. The second query is ``ap", and the answer is 2. The third query is ``c", and the answer is 0. So the total sum is 3+2+0 = 5.
Sample Input
- 4 3
- apple
- apple
- avatar
- book
- 1
- 1 2 2 1 1 1 1 2
- 2
- 1 2 2 1 1 1 1 2
- 10.1 20.1 19.9 20.0 10.2 9.8 9.9 10.0
- 1
- 1 2 2 1 1 1 2 2
Sample Output
- 5
- 题意: 给你一堆商品的名字,然后给你一些条形码,问你这些条形码转换成的字符串的 前缀在商品中出现的个数,条形码的每个字母是八个二进制数字,有两种数,大的是小的2倍,小的是0,大的是1,这里面的吴超是 *0.95---*1.05之间。
思路: 显然是字典树,字典树处理前缀出现次数,先把所有字符串加到树里面,然后我
们想办法吧这个二进制数字翻译成字母,其实很简单,先找到一个最大的,然后枚举每一个 int(max * 1.05 / (num[i] * 0.95)) ,如果他是1,那么当前这位是1,否则当前这位是0 ,然后转换成十进制。然后直接在树上查找就行了。
- #include<iostream>
- #include<stdio.h>
- #include<math.h>
- #include<string.h>
- #include<iostream>
- using namespace std;
- struct trie{
- trie* next[];
- int num;
- trie(){
- for(int i=; i<; i++)
- next[i]=NULL;
- num=;
- }
- } *root=new trie;
- void insert(char* s)
- {
- trie *p=root;
- for(int i=; s[i]!='\0'; i++)
- {
- if(p->next[s[i]-'a']==NULL){
- p->next[s[i]-'a']=new trie;
- p=p->next[s[i]-'a'];
- }
- else{
- p=p->next[s[i]-'a'];
- p->num++;
- }
- }
- }
- int find(char *s){
- trie *p=root;
- for(int i=; s[i]!='\0'; i++)
- if(p->next[s[i]-'a']==NULL)
- return ;
- else
- p=p->next[s[i]-'a'];
- return p->num;
- }
- char str[];
- double a[];
- int b[];
- char str1[];
- int main()
- {
- int n,m;
- while(scanf("%d%d",&n,&m)!=EOF){
- // getchar();
- // for(int i=0;i<26;i++)
- // root->next[i]=NULL;
- // root->num=1;
- for(int i=;i<=n;i++){
- // memset(str,0,sizeof(str));
- scanf("%s",str);
- insert(str);
- }
- int ans=;
- for(int i=;i<=m;i++){
- int k;
- scanf("%d",&k);
- for(int j=;j<k;j++){
- double sum=;
- for(int ii=;ii<;ii++){
- scanf("%lf",&a[ii]);
- sum+=a[ii];
- }
- sum=sum/8.0;
- for(int jj=;jj<;jj++){
- if(a[jj]>sum)
- b[jj]=;
- else
- b[jj]=;
- }
- int s=;
- for(int kk=;kk<;kk++){
- s+=pow(,-kk)*b[kk];
- }
- // memset(str1,0,sizeof(str1));
- str1[j]=char(s);
- }
- str1[k]='\0';
- ans+=find(str1);
- }
- printf("%d\n",ans);
- }
- return ;
- }
UVALive 5029 字典树的更多相关文章
- UVALive 5913 字典树
先输入n个字符串的字典,每个字符串的前缀+后缀可以组成新的合法字符串,但肯定是有重复的,问从给定的字符串,生成的所有可能的字符串为多少个 把前缀和后缀压入字典树,达到前缀和后缀的去重,首先的总和即为前 ...
- UVALive 3942 字典树+dp
其实主要是想学一下字典树的写法,但这个题目又涉及到了DP:这个题目要求某些单词组成一个长子串的各种组合总数,数据量大,单纯枚举复杂度高,首先肯定是要把各个单词给建成字典树,但是之后该怎么推一时没想到. ...
- UVALive 7712 Confusing Manuscript 字典树 查询与s的编辑距离为1的字符串数量
/** 题目:UVALive 7712 Confusing Manuscript 链接:https://vjudge.net/problem/UVALive-7712 题意:给定n个不同的字符串,f( ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- UVALive 3942 Remember the Word(字典树+DP)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive - 3942 (字典树)
递推:$d(i) $表示从第$i$个字符开始到末尾的字符串(即后缀S[i...n])的分解方案数,则$d(i) = \sum {d(i + len(x))} $,其中字符串$x$代表S[i...n]的 ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
随机推荐
- An error occurred at line: 1 in the generated java file问题处理
tomcat6启动后,加载jsp页面报错,提示无法将jsp编译为class文件,主要报错信息如下: An error occurred at line: 1 in the generated java ...
- base_lr, blobs_lr
caffe里面,原来以为是不可以随便调整学习率的,现在看来是可以的.base_lr是适用于所有层的学习率,而针对单个层,可以通过增加两个blobs_lr,用来调整该层的学习率,为什么是两个呢,因为一个 ...
- 3.vue引入axios全局配置
前言: Vue官方推荐使用axios来进行异步访问. axios文档参考:axios中文文档 开始搭建: 1.引入axios (1)打开终端 win+R (2)切换到项目路径: g: cd Webap ...
- lintcode_397_最长上升连续子序列
最长上升连续子序列 描述 笔记 数据 评测 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到 ...
- 【MySQL】使用SQL语句操作MySQL
前言: MySQL在工作中是最常用的数据库,但在使用Django和Flask的时候,都是使用ORM进行操作,除了select语句外,其他的SQL语句操作MySQL的能力没有啥提高,为了解决这个问题, ...
- 深入理解restfulAPI和 Oauth2.0(精简版)
一.restfulAPI 1.解释: restfulAPI协议,我们也可以说是一套API接口编写风格. 它被现在很多企业所认可和默认,是一套成俗的API接口编写方案. 2.restfulAPI之资源 ...
- 学习python第十二天,函数4 生成器generator和迭代器Iterator
在Python中,这种一边循环一边计算的机制,称为生成器:generator 要创建一个generator,有很多种方法.第一种方法很简单,只要把一个列表生成式的[]改成(),就创建了一个genera ...
- Linux基础知识与命令1(su passwd)
一.Linux的基本原则 1.linux由一个个目的单一的小程序组成,我们一般需要组合小程序来完成复杂的任务 2.Linux的一切都是文件(文件类似于一棵树,包括外设,接口) 3.Linux尽量避免捕 ...
- SPOJ1026 概率DP
Favorite Dice BuggyD loves to carry his favorite die around. Perhaps you wonder why it's his favorit ...
- Linux之rsync同步工具介绍+inotify同步
1.rsync介绍 Rsync是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据同步备份的优秀工具.Rsync软件适用于unix/linux/windows等多种操作平台. rsync, ...