标签:
Description
Input
Output
Sample Input
Sample Output
这个题目大意是问给定的按键序列,能在字典里面找到多少个匹配的。
当然首先需要建立一个字母到按键的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; }
字典树版:
#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; }
ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)
标签:
原文地址:http://www.cnblogs.com/andyqsmart/p/4665582.html