在我没用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. 高效程序猿之 VS2010快速生成代码模板

    本文转载,原文地址 http://edi.wang/Post/2011/12/8/efficient-code-monkey-1-vs2010-generate-code-template 一只程序猿 ...

  2. 查询页面checkbox使用

    HTML <input type="checkbox" id="IsChildGroup" name="IsChildGroup" v ...

  3. Myeclipse8.6安装freemarker插件

    1. 打开http://sourceforge.net/projects/freemarker-ide/files/ 下载最新版本,目前本人下载时最新版本是:freemarker-ide-0.9.14 ...

  4. 手机版WEB开发经验分享,手机版网站开发注意事项,网站自适应,手机版网站自适应,移动安卓APP自适应

    转自 http://my.oschina.net/cart/blog/282477 做前端开发不短了,用过jQuery Mobile jqMobi 也纯手工写过.. 最后总结如下: jQuery Mo ...

  5. 全选js实现

    前端全选框 <input type='checkbox' name='allSelect' onclick='sel(this)'></input> //有i个选择框 < ...

  6. 使用composer命令创建laravel项目命令详解

    composer命令创建laravel项目的命令是: composer create-project --prefer-dist laravel/laravel blog "5.2.*&qu ...

  7. 算法:1!+(1!+3!)+(1!+3!+5!) + ( 1! + 3! + 5! + 7! + 9!)+....+(1!+3!+5!+ ... + m!)

    -(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{ / ...

  8. NaN(Not a Number)问题

    Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contai ...

  9. linux cat more less head tail

    cat 命令: cat filename  查看一个文件的内容cat[选项][文件]... -b  对非空白行进行编号,行号从1开始-n  和nl命令差不多,对所有行(包括空白行)进行编号输出显示-E ...

  10. ReactiveCocoa的冷信号与热信号 探讨

    背景 ReactiveCocoa(简称RAC)是最初由GitHub团队开发的一套基于Cocoa的FRP框架.FRP即Functional Reactive Programming(函数式响应式编程), ...