在我没用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. c专家编程---优先级规则

    对于一些复杂的类型组合,总是搞不明白,今天阅读了“优先级规则”这块,有了进一步的理解,特将规则记在此处,供自己学习查询使用. 优先级规则: A.声明从它的名字开始读取,然后按照优先级顺序依次读取 B. ...

  2. flex、As 3.0 小知识

    <s:CheckBox  id="checkBox" x="10" y="5" label="{new ObjectProx ...

  3. Stu Website

    GIT: 分支的新建与合并 https://git-scm.com/book/zh/v2/Git-分支-分支的新建与合并 分支的管理 https://git-scm.com/book/zh/v1/Gi ...

  4. go share library

    http://blog.ralch.com/tutorial/golang-sharing-libraries/ Sharing Golang packages to C and Go Sun, Au ...

  5. asp:Property解释与例子

    =======================================================================Property Get 语句在 Class 块中,声明构 ...

  6. 两种方法将oracle数据库中的一张表的数据导入到另外一个oracle数据库中

    oracle数据库实现一张表的数据导入到另外一个数据库的表中的方法有很多,在这介绍两个. 第一种,把oracle查询的数据导出为sql文件,执行sql文件里的insert语句,如下: 第一步,导出sq ...

  7. oracle多种导入导出数据方法

    dmp格式: 1.dmp格式的导出可以通过客户端工具(PL/SQL)操作来完成,通过菜单栏---->Tools---->Export Tables,然后设置勾选相应参数即可,rows代表是 ...

  8. The Rings Akhaten

    在其他的平行宇宙中存在着一个古老的星系--Akhaten,星系中有七个世界,上面生活着Panbabylonian.Lucanian等物种,不过外界也常常把他们统称为Akhet,因为这七个世界环绕着同一 ...

  9. popoverController(iPad)

    一.设置尺寸 提示:不建议,像下面这样吧popover的宽度和高度写死. 1 //1.新建一个内容控制器 2 YYMenuViewController *menuVc=[[YYMenuViewCont ...

  10. HDU2066一个人的旅行/最短路问题

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...