HDU - 4287 Intelligent IME

HDU - 4287

Intelligent IME

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4904 Accepted Submission(s): 2295

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个数字中出现的次数。

思路:因为是一一对应关系,所以想到用map。但需要的内存较大。注意cnt数组要开1e7,cnt[i]存的是单词所对应的数字i的出现次数。

#include<cstdio>
#include<cstring>
#include<map>

using namespace std;

const int maxn = 5000 + 5;

map<char,int>dic;
int cnt[1000000];

int main()
{
    dic['a']=dic['b']=dic['c']=2;
    dic['d']=dic['e']=dic['f']=3;
    dic['g']=dic['h']=dic['i']=4;
    dic['j']=dic['k']=dic['l']=5;
    dic['m']=dic['n']=dic['o']=6;
    dic['p']=dic['q']=dic['r']=dic['s']=7;
    dic['t']=dic['u']=dic['v']=8;
    dic['w']=dic['x']=dic['y']=dic['z']=9;
    int t,n,m;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        getchar();
        memset(cnt,0,sizeof(cnt));
        int num[maxn];
        for(int i = 0; i < n; i++){
            scanf("%d",&num[i]);
        }
        getchar();
        for(int i = 0; i < m; i++){
            char s[10];
            scanf("%s",s);
            int len = strlen(s);
            int times = 1, tmp = 0;
            for(int j = len-1; j >= 0; j--){//求该单词对应的数字
                tmp += dic[s[j]]*times;
                times *= 10;
            }
            cnt[tmp]++;
        }
        for(int i = 0; i < n; i++){
            printf("%d\n",cnt[num[i]]);
        }
    }
    return 0;
}

另一种方法是用字典树。

字典树,又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。

字典树就像字典一样,当你要查一个单词是不是在字典树中,首先看单词的第一个字母是不是在字典的第一层,如果不在,说明字典树里没有该单词,如果在就在该字母的孩子节点里找是不是有单词的第二个字母,没有说明没有该单词,有的话用同样的方法继续查找。字典树不仅可以用来储存字母,也可以储存数字等其它数据。

#include<cstdio>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>

using namespace std;

typedef long long LL;
const int MAXN = 5000 + 5;
const int MAXM = 1000000 + 5;

struct node {
    int v,s,next[30];
    void init() {
        v = -1;
        s = 0;
        memset(next, -1, sizeof(next));
    }
} L[MAXM];

int N, M, T, tot;
char str[MAXN][10],op[10];


int Get_Num(char ch) {
    if(ch >= 'a' && ch <= 'c') return 2;
    if(ch >= 'd' && ch <= 'f') return 3;
    if(ch >= 'g' && ch <= 'i') return 4;
    if(ch >= 'j' && ch <= 'l') return 5;
    if(ch >= 'm' && ch <= 'o') return 6;
    if(ch >= 'p' && ch <= 's') return 7;
    if(ch >= 't' && ch <= 'v') return 8;
    if(ch >= 'w' && ch <= 'z') return 9;
}

int add(char *a, int len) {//返回出现次数
    int now = 0;
    for(int i = 0; i < len; i++) {//把节点一个个加进去
        int tmp = a[i] - '0';
        int next = L[now].next[tmp];
        if(next == -1) {
            next = ++tot;
            L[next].init();
            L[now].next[tmp] = next;
        }
        now = next;
    }
    L[now].v = 0;
    return L[now].s;
}


void query(char * a, int len) {
    int now = 0;
    for(int i = 0; i < len; i ++) {
        int tmp = Get_Num(a[i]);
        int next = L[now].next[tmp];
        if(next == -1) return; //无子节点,查找失败
        now = next;
    }
    if(L[now].v == 0) //查找成功,s++
        L[now].s ++;
}

int main() {
    scanf("%d", &T);
    while(T --) {
        tot = 0;
        L[0].init();
        scanf("%d%d", &N, &M);
        for(int i = 0; i < N; i ++) {
            scanf("%s", str[i]);
            add(str[i], strlen(str[i]));//把数字串加入字典树中
        }
        for(int i = 0 ; i < M; i ++) {
            scanf("%s", op);
            query(op, strlen(op));//在字典树中查找
        }
        for(int i = 0; i < N ; i ++) {
            printf("%d\n", add(str[i], strlen(str[i])));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值