首页 技术 正文
技术 2022年11月8日
0 收藏 447 点赞 1,464 浏览 3794 个字

Description

  We all use cell phone today. And we must be familiar with the intelligent English input method on the cell phone. To be specific, the number buttons may correspond to some English letters respectively, as shown below:
  2 : a, b, c    3 : d, e, f    4 : g, h, i    5 : j, k, l    6 : m, n, o    

  7 : p, q, r, s  8 : t, u, v    9 : w, x, y, z
  When we
want to input the word “wing”, we press the button 9, 4, 6, 4, then the
input method will choose from an embedded dictionary, all words
matching the input number sequence, such as “wing”, “whoi”, “zhog”. Here
comes our question, given a dictionary, how many words in it match some
input number sequences?  

Input

  First is an integer T, indicating the number of test cases.
Then T block follows, each of which is formatted like this:

  Two integer N (1 <= N <= 5000), M (1 <= M <=
5000), indicating the number of input number sequences and the number of
words in the dictionary, respectively. Then comes N lines, each line
contains a number sequence, consisting of no more than 6 digits. Then
comes M lines, each line contains a letter string, consisting of no more
than 6 lower letters. It is guaranteed that there are neither
duplicated number sequences nor duplicated words.  

Output

  For each input block, output N integers, indicating how many
words in the dictionary match the corresponding number sequence, each
integer per line.  

Sample Input

13 5466444874goinnightmightgn  

Sample Output

320

这个题目大意是问给定的按键序列,能在字典里面找到多少个匹配的。

当然首先需要建立一个字母到按键的Hash,可以使用map也可以使用数组,因为字母的ASCII值不是很大。

这样就能把字典里面的字母序列映射成数字序列了。

然后就是对查询的序列和字典里面对应的Hash序列进行匹配。

这个匹配可以依旧使用map进行映射,map的存入的是序列在字典里面出现的次数。效率是N*logM。

此外,由于是数字的匹配,同样可以看作是字符串匹配,自然可以使用字典树。效率是N*strlen(str),其中strlen指的是平均查询的字符串长度。

可见当M很大时,字典树要快一点。

当然在使用字典树的时候最好释放掉内存,不然内存泄漏,内存消耗很大。

代码:

map版:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long longusing namespace std;char hashStr[][] = {
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};
map<char, int> Hash;
int n, m;
map<int, int> dic;
int query[];void init()
{
for (int i = ; i < ; ++i)
{
int len = strlen(hashStr[i]);
for (int j = ; j < len; ++j)
Hash[hashStr[i][j]] = i+;
}
}int turn(char str[])
{
int num = , len = strlen(str);
for (int i = ; i < len; ++i)
num = *num + Hash[str[i]];
return num;
}void input()
{
dic.clear();
char str[];
scanf("%d%d", &n, &m);
for (int i = ; i < n; ++i)
scanf("%d", &query[i]);
for (int i = ; i < m; ++i)
{
scanf("%s", str);
dic[turn(str)]++;
}
}void work()
{
for (int i = ; i < n; ++i)
{
if (dic[query[i]])
printf("%d\n", dic[query[i]]);
else
printf("0\n");
}
}int main()
{
//freopen("test.in", "r", stdin);
init();
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
input();
work();
}
return ;
}

字典树版:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
#define LL long longusing namespace std;char hashStr[][] = {
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};
map<char, int> Hash;
int n, m;
char query[][];//字典树
struct Tree
{
int num;
Tree *next[];
} *head;void add(char str[])
{
Tree *p = head;
int k, len = strlen(str);
for (int i = ; i < len; ++i)
{
k = str[i]-'';
if (p->next[k] == NULL)
{
Tree *q = (Tree *)malloc(sizeof(Tree));
q->num = ;
for (int i = ; i <= ; ++i)
q->next[i] = NULL;
p->next[k] = q;
}
p = p->next[k];
}
p->num++;
}int Query(char str[])
{
Tree *p = head;
int k, len = strlen(str);
for (int i = ; i < len; ++i)
{
k = str[i]-'';
if (p->next[k] == NULL)
return ;
p = p->next[k];
}
return p->num;
}void del(Tree *p)
{
for (int i = ; i <= ; ++i)
{
if (p->next[i] != NULL)
del(p->next[i]);
}
free(p);
}void init()
{
for (int i = ; i < ; ++i)
{
int len = strlen(hashStr[i]);
for (int j = ; j < len; ++j)
Hash[hashStr[i][j]] = i+;
}
}void turn(char str[])
{
int num = , len = strlen(str);
for (int i = ; i < len; ++i)
str[i] = Hash[str[i]]+'';}void input()
{
head = (Tree *)malloc(sizeof(Tree));
for (int i = ; i <= ; ++i)
head->next[i] = NULL;
char str[];
scanf("%d%d", &n, &m);
for (int i = ; i < n; ++i)
scanf("%s", query[i]);
for (int i = ; i < m; ++i)
{
scanf("%s", str);
turn(str);
add(str);
}
}void work()
{
for (int i = ; i < n; ++i)
printf("%d\n", Query(query[i]));
}int main()
{
//freopen("test.in", "r", stdin);
init();
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
input();
work();
del(head);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,492
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,293