在我没用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. 在Activity之间传递数据—获取Activity返回的数据

    在获取返回值时要注意的是打开Activity的方式,用方法:startActivityForResult 接收时,重写方法:onActivityResult 在子Activity中,写数据用方法:se ...

  2. OpenCV成长之路:图像直方图的应用

    OpenCV成长之路:图像直方图的应用 2014-04-11 13:57:03 标签:opencv 图像 直方图 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否 ...

  3. JS总结之一:字符串的调用方法

    字符串的调用方法:var s="hello, world";document.write(s.charAt(0)); //第一个字符document.write(s.charAt( ...

  4. vim编程配置方法

    vim简介Vim 有以下几个模式:1) 正常(normal)模式,缺省的编辑模式:下面如果不加特殊说明,提到的命令都直接在正常模式下输入:任何其它模式中都可以通过键盘上的 Esc 键回到正常模式.2) ...

  5. liunx 内存文件 tmpfs

    tmpfs是Linux/Unix系统上的一种基于内存的文件系统.tmpfs可以使用您的内存或swap分区来存储文件 1 创建将被挂载的文件 mkdir /tmp/tmpfs/ 2 写入测试文件内容.大 ...

  6. -Swift.h not find

    亲测成功. 随便新建一个swift文件,xcode问是否生成xxx-Bridging-Header.h文件,点YES.再编译,问题解决. By default, the generated heade ...

  7. wifi的UI控制。打不开UI

    部分主要是您要清楚WiFi作为什么功能,即WiFi是仅仅作为station功能,还是要开启p2p功能,当WiFi作为station功能时候开启的是wpa_supplicant服务,当WiFi作为p2p ...

  8. 在ASP.Net中"../" "./" "~/"表示的意思

    "../" 相对当前(根据不同的语境,当前是不同的)的上一级目录"./" 就是当前目录 "~/" 当前项目的根路径,只在服务器端有效.

  9. php moungoDB

    moungoDB 语法 SQL查询语句 Mongo查询语句 CREATE TABLE USERS (a Number, b Number) 隐式的创建,或 MongoDB::createCollect ...

  10. linux(视频学习)2

    第二部分(javaee的开发环境的搭建): 1. 安装jdk的过程: 安装ios的镜像文件,挂载到/mnt目录下.挂载: mount  /mnt/cdrom卸载: umount  /mnt/cdrom ...