Problem 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
1
3 5
46
64448
74
go
in
night
might
gn
 
Sample Output
3
2
0

题意与思路:

输入N个按键序列, 再输入M个单词, 要求我们对M个单词进行检索, 求出其按键序列

然后统计, N个按键序列每个分别出现了几次

错误点: 由于不注意严谨, 以为少写一个break没事, 导致超时~

我有两份代码, 一份是错误的, 还没找出错误来, 一份已AC;

AC代码:

#include<stdio.h>
#include<string.h> int t[26] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9}; int T, N, M; int ans[5555];
char num[5555][10];
char word[5555][10]; void getans(char *p) {
int i, len;
int pos = 0;
char t_num[10];
len = strlen(p);
for(i = 0; i < len; i++)
t_num[pos++] = t[p[i]-'a'] + '0';
t_num[pos] = '\0';
for(i = 0; i < N; i++)
if(strcmp(t_num, num[i]) == 0) {
ans[i]++;
break;
}
} int main() {
scanf("%d", &T);
while(T--) {
int i;
scanf("%d %d", &N, &M); getchar(); memset(ans, 0, sizeof(ans));
for(i = 0; i < N; i++)
gets(num[i]);
for(i = 0; i < M; i++)
gets(word[i]); for(i = 0; i < M; i++)
getans(word[i]); for(i = 0; i < N; i++)
printf("%d\n", ans[i]);
}
return 0;
}

wrong代码:

#include<stdio.h>
#include<string.h> int T, N, M; char num[5555][10];
char word[5555][10];
int ans[5555]; char t[10][10]; void getans(char *p) {
int i, j, k, pos = 0;
char t_num[10];
int len1, len2;
len1 = strlen(p);
for(i = 0; i < len1; i++) {
for(j = 2; j < 10; j++) {
len2 = strlen(t[j]);
for(k = 0; k < len2; k++) {
if(t[j][k] == p[i])
t_num[pos++] = j;
}
}
} t_num[pos] = '\0';
for(i = 0; i < N; i++) {
if(strcmp(t_num, num[i]) == 0)
ans[i]++;
}
} int main() {
scanf("%d", &T); while(T--) {
int i;
scanf("%d%d", &N, &M); memset(ans, 0, sizeof(ans));
strcpy(t[2], "abc");
strcpy(t[3], "def");
strcpy(t[4], "ghi");
strcpy(t[5], "jkl");
strcpy(t[6], "mno");
strcpy(t[7], "pqrs");
strcpy(t[8], "tuv");
strcpy(t[9], "wxyz"); for(i = 0; i < N; i++)
gets(num[i]);
for(i = 0; i < M; i++)
gets(word[i]); for(i = 0; i < M; i++)
getans(word[i]); for(i = 0; i < N; i++)
printf("%d\n", ans[i]);
}
return 0;
}

HDU 4287 (13.08.17)的更多相关文章

  1. UVA 536 (13.08.17)

     Tree Recovery  Little Valentine liked playing with binary trees very much. Her favoritegame was con ...

  2. UVA 673 (13.08.17)

     Parentheses Balance  You are given a string consisting of parentheses () and []. Astring of this ty ...

  3. HDU 4642 (13.08.25)

    Fliping game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  4. HDU 4287 Intelligent IME(字典树数组版)

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. 13.1.17 CREATE TABLE Syntax

    13.1.17 CREATE TABLE Syntax 13.1.17.1 CREATE TABLE ... LIKE Syntax 13.1.17.2 CREATE TABLE ... SELECT ...

  6. Intel Digital Innovation Industry Summit(2018.08.17)

    时间:2018.08.17地点:北京金隅喜来登大酒店

  7. HDU 4287 Intelligent IME hash

    Intelligent IME Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  8. hdu 4287

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4287 #include<cstdio> #include<cstring> # ...

  9. UVA 10194 (13.08.05)

    :W Problem A: Football (aka Soccer)  The Problem Football the most popular sport in the world (ameri ...

随机推荐

  1. 理解JavaScript 的原型属性

    1.原型继承 面向对象编程可以通过很多途径实现.其他的语言,比如 Java,使用基于类的模型实现: 类及对象实例区别对待.但在 JavaScript 中没有类的概念,取而代之的是一切皆对象.JavaS ...

  2. CSS结构伪类E:first-child/last-child/only-child/empty

    E:first-child解释:E的父元素的第一个子元素正好是E,给这个E定义样式 E:last-child解释:E的父元素的最后一个子元素正好是E,给这个E定义样式 E:only-child解释:E ...

  3. (转).net开发者对android第二周的学习体会

    这一周相对没有春节时这么闲了,白天也比较多的工作要做,每天晚上又要被我三岁的女儿折腾到十点, 实在没有多少时间学习.在前一周的基础上,这周我试着自己练习写了一个个人管理的android的程序,主要实现 ...

  4. js获取上传文件扩展名

    File_box.value.substring(File_box.value.lastIndexOf(".") + 1);

  5. oracle包概述(一)【weber出品】

    一.PL/SQL包概述 1. 什么是PL/SQL包: 相关组件的组合:PL/SQL类型.变量,数据结构,和表达式.子程序: 过程和函数 2. 包的组成部分: 由两部分组成: 包头 包体 3. 包的优点 ...

  6. 控制反转IOC与依赖注入DI【转】

    转自:http://my.oschina.net/1pei/blog/492601 一直对控制反转.依赖注入不太明白,看到这篇文章感觉有点懂了,介绍的很详细. 1. IoC理论的背景我们都知道,在采用 ...

  7. [转载]opencv +linux

    转载 ubuntu12.04安装openCV2.4.2(2012-08-08 16:54:06 参考http://www.samontab.com/web/2012/06/installing-ope ...

  8. 使用ganymed-ssh2-build通过ssh获得远程服务器参数

    1.项目中需要检测到几台远程服务器的参数,差了很多资料,决定用的这个 2.jar包:ganymed-ssh2-build210.jar 3.原理:向远程linux服务器发送脚本命令,得到该台服务器的信 ...

  9. Largest Rectangle in a Histogram(HDU 1506 动态规划)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  10. Linux_Shell type

    Recommendation is to use the bash shell, because he is strong enough, and absorbed the useful proper ...