HDU 4287 Intelligent IME(字典树)
在我没用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(字典树)的更多相关文章
- ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)
Description We all use cell phone today. And we must be familiar with the intelligent English input ...
- HDU 4287 Intelligent IME(map运用)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4287 Intellig ...
- HDU 4287 Intelligent IME(字典树数组版)
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4287 Intelligent IME hash
Intelligent IME Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 4287 Intelligent IME
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4287 Intelligent IME(string,map,stl,make_pair)
题目 转载来的,有些stl和string的函数蛮好的: //numx[i]=string(sx); //把char[]类型转换成string类型 // mat.insert(make_pair(num ...
- hdu 1671 Phone List 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
随机推荐
- apk的重签名
1. 生成Android APK包签名证书 1). 在doc中切换到jdk的bin目录 cd C:\Program Files\Java\jdk1.6.0_18\bin 2). ...
- Codeforces Round #364 (Div. 2) D. As Fast As Possible
D. As Fast As Possible time limit per test 1 second memory limit per test 256 megabytes input stand ...
- 禁止 一些地区的ip用户访问 网站
01 framework.php 中 获取请求的ip地址.保存为常量 define('IP', request::get_clientip()); 02 setting表中.属于system模块,保存 ...
- string的数值转换
to_string(val); //数值val的string表示 stoi (s, p, b); stol (s, p, b); stoul (s, p, b); stoll (s, p, b); s ...
- H5调用本地摄像头
<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"& ...
- c#操作oracle的通用类
using System;using System.Collections;using System.Collections.Generic;using System.Data;using Syste ...
- MySQL 索引 总结
1.索引的种类(六种) 普通索引,唯一索引,全文索引,单列索引,多列索引,空间索引 2.优缺点及注意事项 优点:有了索引,对于记录数量很多的表,可以提高查询速度. 缺点:索引是占用空间的,索引会影响u ...
- asp之FSO大全
<%Function ShowDriveInfo(strFolder)'显示磁盘信息'strRootFolder="/"'strDrivInfo=ShowDriveInfo( ...
- C++ concepts: Compare
The concept Compare is a set of requirements expected by some of the standard library facilities fro ...
- A convenient way of installing(compiling) VIM with YCM
Ah, while I am still downloading LLVM from github(very slow.. and very large in size). I come with m ...