ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

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

 

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

当然首先需要建立一个字母到按键的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 long

using namespace std;

char hashStr[][10] = {
    "abc",
    "def",
    "ghi",
    "jkl",
    "mno",
    "pqrs",
    "tuv",
    "wxyz"
};
map<char, int> Hash;
int n, m;
map<int, int> dic;
int query[5005];

void init()
{
    for (int i = 0; i < 8; ++i)
    {
        int len = strlen(hashStr[i]);
        for (int j = 0; j < len; ++j)
            Hash[hashStr[i][j]] = i+2;
    }
}

int turn(char str[])
{
    int num = 0, len = strlen(str);
    for (int i = 0; i < len; ++i)
        num = 10*num + Hash[str[i]];
    return num;
}

void input()
{
    dic.clear();
    char str[10];
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i)
        scanf("%d", &query[i]);
    for (int i = 0; i < m; ++i)
    {
        scanf("%s", str);
        dic[turn(str)]++;
    }
}

void work()
{
    for (int i = 0; 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 = 0; times < T; ++times)
    {
        input();
        work();
    }
    return 0;
}
View Code

字典树版:

#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 long

using namespace std;

char hashStr[][10] = {
    "abc",
    "def",
    "ghi",
    "jkl",
    "mno",
    "pqrs",
    "tuv",
    "wxyz"
};
map<char, int> Hash;
int n, m;
char query[5005][10];

//字典树
struct Tree
{
    int num;
    Tree *next[11];
} *head;

void add(char str[])
{
    Tree *p = head;
    int k, len = strlen(str);
    for (int i = 0; i < len; ++i)
    {
        k = str[i]-'0';
        if (p->next[k] == NULL)
        {
            Tree *q = (Tree *)malloc(sizeof(Tree));
            q->num = 0;
            for (int i = 2; i <= 9; ++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 = 0; i < len; ++i)
    {
        k = str[i]-'0';
        if (p->next[k] == NULL)
            return 0;
        p = p->next[k];
    }
    return p->num;
}

void del(Tree *p)
{
    for (int i = 2; i <= 9; ++i)
    {
        if (p->next[i] != NULL)
            del(p->next[i]);
    }
    free(p);
}

void init()
{
    for (int i = 0; i < 8; ++i)
    {
        int len = strlen(hashStr[i]);
        for (int j = 0; j < len; ++j)
            Hash[hashStr[i][j]] = i+2;
    }
}

void turn(char str[])
{
    int num = 0, len = strlen(str);
    for (int i = 0; i < len; ++i)
        str[i] = Hash[str[i]]+'0';

}

void input()
{
    head = (Tree *)malloc(sizeof(Tree));
    for (int i = 2; i <= 9; ++i)
        head->next[i] = NULL;
    char str[10];
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i)
        scanf("%s", query[i]);
    for (int i = 0; i < m; ++i)
    {
        scanf("%s", str);
        turn(str);
        add(str);
    }
}

void work()
{
    for (int i = 0; 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 = 0; times < T; ++times)
    {
        input();
        work();
        del(head);
    }
    return 0;
}
View Code

 

posted on 2015-07-21 20:46  AndyQsmart  阅读(259)  评论(0编辑  收藏  举报

导航