Intelligent IME

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

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

题目链接:HDU 4287

由于最近要学AC自动机的缘故,指针版的字典树虽然好写但是速度慢且容易爆内存,于是就膜了一下数组版的字典树,略微麻烦了一丢丢,但是可操作性比指针的高,速度比指针的快非常多……,于是就拿这题试验了一下,数组版字典树是用下标来替代指针的作用,而且空间都是预先分配好的,只要不把下标标向某一层,这一层就不会被用到,就相当于未出现,没有被分配内存。tot表示当前节点个数,显然一开始tot=1,因为有一个表头(对应于链表里的Nodelist *L),且表头的数组下标刚好是0,每一次分配就把L[tot]这一层的内容给初始化,再把这个连到L[now]->nxt[v]上即可。这题做法非常简单,就是问你每一个按键顺序可以分别打出下面几个字符串,显然把字典树结构体中加一个id表示当前是第几组按键,再把接下来的m个字符转换成对应的按键顺序再拿去查询,返回最后查到的那个id再把这个对应的按键次数+1即可。对了这个空间复杂度大概就开max_len * Tot_cnt就大概可以了,具体不清楚到底要开多少,反正暂时开这么多似乎题目都还可以过。话说把init作为成员函数里面还真是方便…………

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=5010;
struct Trie
{
int nxt[10];
int id;
inline void init()
{
CLR(nxt,0);
id=-1;
}
};
Trie L[N*7];
int tot,ans[N];
char s[10];
char change[10]; void init()
{
L[0].init();
tot=1;
CLR(ans,0);
}
void update(char s[],int id)
{
int now=0,len=strlen(s);
for (int i=0; i<len; ++i)
{
int v=s[i]-'0';
if(!L[now].nxt[v])
{
L[tot].init();
L[now].nxt[v]=tot++;
}
now=L[now].nxt[v];
}
L[now].id=id;
}
int Find(char s[])
{
int now=0;
int len=strlen(s);
for (int i=0; i<len; ++i)
{
int v=s[i]-'0';
if(!L[now].nxt[v])
return -1;
now=L[now].nxt[v];
}
return L[now].id;
}
inline int getid(const char &x)
{
if(x>='a'&&x<='c')
return 2;
else if(x>='d'&&x<='f')
return 3;
else if(x>='g'&&x<='i')
return 4;
else if(x>='j'&&x<='l')
return 5;
else if(x>='m'&&x<='o')
return 6;
else if(x>='p'&&x<='s')
return 7;
else if(x>='t'&&x<='v')
return 8;
else
return 9;
}
int main(void)
{
int tcase,n,m,i;
scanf("%d",&tcase);
while (tcase--)
{
init();
scanf("%d%d",&n,&m);
for (i=0; i<n; ++i)
{
scanf("%s",s);
update(s,i);
}
for (i=0; i<m; ++i)
{
scanf("%s",s);
int len=strlen(s);
for_each(s,s+len,[](char &c){c=getid(c)+'0';});
int indx=Find(s);
if(indx!=-1)
++ans[indx];
}
for_each(ans,ans+n,[&](const int &c){printf("%d\n",c);});
}
return 0;
}

HDU 4287 Intelligent IME(字典树数组版)的更多相关文章

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

    Description We all use cell phone today. And we must be familiar with the intelligent English input ...

  2. HDU 4287 Intelligent IME(map运用)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4287 Intellig ...

  3. HDU 4287 Intelligent IME(字典树)

    在我没用hash之前,一直TLE,字符串处理时间过长,用了hash之后一直CE,(请看下图)我自从经历我的字典树G++MLE,C++AC以后,一直天真的用C++,后来的CE就是因为这个,G++才支持这 ...

  4. HDU 4287 Intelligent IME hash

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

  5. HDU 4287 Intelligent IME

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

  6. HDU 4287 Intelligent IME(string,map,stl,make_pair)

    题目 转载来的,有些stl和string的函数蛮好的: //numx[i]=string(sx); //把char[]类型转换成string类型 // mat.insert(make_pair(num ...

  7. hdu 1671 Phone List 字典树

    // hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...

  8. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  9. HDU 5536 Chip Factory 字典树

    Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

随机推荐

  1. mongodb学习05 操作详解(3)

    高级查询选项 //简单查询 var cursor = db.foo.find({"foo" : "bar"}) //封装查询 var cursor = db.f ...

  2. 服装PDA软件|服装销售开单PDA管理软件|服装PDA管理系统|服装PDA点货系统|服装移动POS系统

    浩瀚软件为了更好服务于广大批发客户群体进行销售.盘点.调拨配送等.深圳浩瀚软件推出基于无线网络版移动PDA销售开单系统.该系统支持无线3G.WIFI.GPRS系统,用户可以手持PDA在无线网络连接状态 ...

  3. css3 -- 背景图处理

    1.多背景图片: p{ background-image:url() , url(); background-position:95% 90% , 50% 50%; background-repect ...

  4. TStringList 常用操作

    //TStringList 常用方法与属性: var   List: TStringList;   i: Integer; begin   List := TStringList.Create;   ...

  5. FastMM 定位内存泄露的代码位置

    FastMM 定位内存泄露的代码位置 开源的FastMM,使用很简单,在工程的第一行引用FastMM4即可(注意,一定要在第一个Uses的位置),可以在调试程序时提示内存泄露情况,还可以生成报告. 在 ...

  6. 树状数组 + 位运算 LA 4013 A Sequence of Numbers

    题目传送门 题意:n个数,两种操作,一是每个数字加x,二是查询& (1 << T) == 1 的个数 分析:因为累加是永远的,所以可以离线处理.树状数组点是c[16][M] 表示数 ...

  7. 使用sqoop 在关系型数据库和Hadoop之间实现数据的抽取

    (一)从关系型数据库导入至HDFS 1.将下面的参数保持为 import.script import --connectjdbc:mysql://192.168.1.14:3306/test--use ...

  8. iOS 'The sandbox is not sync with the Podfile.lock错误

    出现以下错误时, diff: /../Podfile.lock: No such file or directory diff: Manifest.lock: No such file or dire ...

  9. AdaBoost 和 Real Adaboost 总结

    AdaBoost 和 Real Adaboost 总结 AdaBoost Real AdaBoost AdaBoost AdaBoost, Adaptive Boosting(自适应增强), 是一种集 ...

  10. BZOJ4665 : 小w的喜糖

    考虑枚举哪些人一定不合法,那么方案数可以通过简单的排列组合算出. 于是设$f[i][j]$表示前$i$种糖果,一共有$j$个人一定不合法的方案数,但是这样并不能保证其他人一定合法,所以需要进行容斥. ...