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

#include<cstdio>
#include<iostream>
#include<string.h>
int hash[];
struct TrieNode
{
int no;
TrieNode *next[];
} node[];
TrieNode *root = &node[];
int cnt,result[];
char word[],s[];
void init()
{
hash['a']=hash['b']=hash['c']=;
hash['d']=hash['e']=hash['f']=;
hash['g']=hash['h']=hash['i']=;
hash['j']=hash['k']=hash['l']=;
hash['m']=hash['n']=hash['o']=;
hash['p']=hash['q']=hash['r']=hash['s']=;
hash['t']=hash['u']=hash['v']=;
hash['w']=hash['x']=hash['y']=hash['z']=;
}
void initRoot()
{
int i;
for(i=; i<; i++)
{
root->next[i]=NULL;
}
}
void insert(char str[],int num)
{
TrieNode *p = root;
int len=strlen(str),i,j;
for(i=; i<len; i++)
{
if(p->next[str[i]-'']==NULL)
{
p->next[str[i]-'']=&node[cnt];
for(j=; j<; j++)node[cnt].next[j]=NULL;
node[cnt].no=-;
cnt++;
}
p=p->next[str[i]-''];
}
p->no=num;
}
/** 查询一个字母字符串对应的数字串 */
void query(char str[])
{
int len=strlen(str),i;
TrieNode *p=root;
for(i=; i<len; i++)
{
p=p->next[hash[str[i]]];
if(p==NULL)break;
}
if(p==NULL)return;
else
{
if(p->no!=-)result[p->no]++;
}
}
int main()
{
int t,m,n,i;
scanf("%d",&t);
init();
while(t--)
{
cnt=;
initRoot();
memset(result,,sizeof(result));
scanf("%d%d",&n,&m);
for(i=; i<n; i++)
{
scanf("%s",word);
insert(word,i);
}
for(i=; i<m; i++)
{
scanf("%s",s);
query(s);
}
for(i=; i<n; i++)
{
printf("%d\n",result[i]);
}
}
return ;
}

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(字典树数组版)

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

  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. Chapter 2 Open Book——10

    I sent that, and began again. 我发送了它,然后又一次重新开始写了. Mom,Everything is great. Of course it's raining. I ...

  2. hdu_3966_Aragorn's Story(树链剖分裸题)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给你一棵树,然后给定点之间的路径权值修改,最后单点查询 题解:树链剖分裸题,这里我用树状数 ...

  3. hdu_3001_Travelling(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题意:给你N个点,M条边,每个点最多走两次,问走完N个点最短的路程为多少. 题解:注意这题有重边 ...

  4. Django:之传递数据给JS、Ajax和Ajax CSRF认证

    Django传递数据给JS 有时候我们想把一个list或者dict传递给javascript,处理后显示到网页上,比如要用js进行可视化到数据. 请注意:如果是不处理,直接显示在网页上,用Django ...

  5. virtualenv 管理python 环境

    virualenvvirtualenv用于创建独立的Python环境,多个Python相互独立,互不影响,它能够:1. 在没有权限的情况下安装新套件2. 不同应用可以使用不同的套件版本3. 套件升级不 ...

  6. JavaScript 伪造 Referer 来路方法

    Javascript 是一种由Netscape的LiveScript发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如Perl,遗留的速度问题, ...

  7. 设计一种前端数据延迟加载的jQuery插件(2)

    背景 最近看到很多网站都运用到了一种前端数据延迟加载技术,包括淘宝,新浪网等等,这样做的目的可以使得一些未显示的图片随 着滚动条的滚动进行延迟显示. 好处显而易见,可以减少前端对于图片的Http请求, ...

  8. event小解

    首先是一个小例子: <input type="text" onclick="a(event)"/> function a(event){   con ...

  9. 转 vi 技巧和诀窍:令人刮目相看的 10 个超酷命令

    for ksh vi 编辑器的许多选项可以控制编辑会话的外观和感觉.使用 :set 命令修改 vi 中的会话设置.按 Escape 键进入命令模式之后,可以使用 :set all 命令显示选项和设置的 ...

  10. POJ1308 Is It A Tree?

    题目大意:和HDU1272-小希的迷宫题目一样, 如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径 ...