HDU 4287 (13.08.17)
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?
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.
3 5
46
64448
74
go
in
night
might
gn
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)的更多相关文章
- UVA 536 (13.08.17)
Tree Recovery Little Valentine liked playing with binary trees very much. Her favoritegame was con ...
- UVA 673 (13.08.17)
Parentheses Balance You are given a string consisting of parentheses () and []. Astring of this ty ...
- HDU 4642 (13.08.25)
Fliping game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- HDU 4287 Intelligent IME(字典树数组版)
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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 ...
- Intel Digital Innovation Industry Summit(2018.08.17)
时间:2018.08.17地点:北京金隅喜来登大酒店
- HDU 4287 Intelligent IME hash
Intelligent IME Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- hdu 4287
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4287 #include<cstdio> #include<cstring> # ...
- UVA 10194 (13.08.05)
:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ...
随机推荐
- [RxJS] Refactoring Composable Streams in RxJS, switchMap()
Refactoring streams in RxJS is mostly moving pieces of smaller streams around. This lessons demonstr ...
- 生命周期-初识IOS
经常因为生命周期的事情,而视图顺序加载错误,或者出现一系列的小错误并且修改不出来,程序员不知道生命周期确实挺可悲的. IOS生命周期: 自上而下的执行,并且viewDidLoad只会执行一次,所以我们 ...
- <经验杂谈>C#/.Net字符串操作方法小结
字符串操作是C#中最基本的.最常见的.也是用的最多的,以下我总结 了几种常见的方法 1.把字符串按照分隔符转换成 List /// <summary> /// 把字符串按照分隔符转换成 L ...
- Link all references for a local rename (does not change references in other files)
这是一个十分easy的问题 错误原因 : import Android.R; 把这句话去掉 ctrl + shift+o 引进一个 项目对应的R文件 R存在于gen文件夹下 可以找到 layo ...
- html5新特性--音频视频,拖放
1.音频 <audio controls> <source src="aaa.ogg" type="audio/ogg"> <so ...
- BZOJ 3277 串 (广义后缀自动机)
3277: 串 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 309 Solved: 118 [Submit][Status][Discuss] De ...
- ANSI与UINCODE编码
简要说明: ANSI是一种字符代码,为使计算机支持更多语言,通常使用 0x80~0xFF 范围的 2 个字节来表示 1 个字符. Uincode(统一码.万国码.单一码)是计算机科学领域里的一项业界标 ...
- C/C++中的函数传值
一.运行如下程序段 #include <iostream> #include <string> #include <cstring> //strcpy #inclu ...
- 对于JavaScript对象的prototype和__proto__的理解
一.Object和Function的关系: 刚学JavaScript的时候,看书上说JavaScript中万物皆对象,而javascript中的其他对象都是从Object继承而来,包括内置对象.瞬间觉 ...
- python文件_目录
#! /usr/bin/env python #coding=gbk import os import time #设置文件的默认路径,当指定的目录不存在时,引发异常:WindowsError:[er ...