您的位置:首页 > 其它

hihoCoder hiho一下 第二周 #1014 : Trie树(Trie树基本应用)

2015-04-20 23:38 501 查看
思路:

  完全看题目中的介绍就行了。还有里面的input写道:不保证是英文单词,也有可能是火星文单词哦。比赛结束后的提交是不用考虑26个字母之外的,都会AC,如果考虑128种可能的话,爆了内存。步骤就是,在插单词的同时记录该结点之后的单词数,查词就查最后一个字母所在结点上的单词数。

#include <cstdio>
#include <cstring>

const int MAXL = 10 + 2;
const int MAXN = 1000 + 10;
const int MAXM = 100000 + 10;

int next[MAXN * MAXL][26], count[MAXN * MAXL];

int main() {
int n;
while (scanf("%d", &n) != EOF) {
int top = 1; memset(next, 0, sizeof(next));
char str[MAXL];
for (int i = 0; i < n; i++) {
scanf("%s", str);
int p = 1;
for (int j = 0; str[j] != '\0'; j ++) {
if (next[p][str[j] - 'a'] == 0) next[p][str[j] - 'a'] = ++top;
p = next[p][str[j] - 'a'];
count[p] ++;
}
}
int m;
scanf("%d", &m);
while (m --) {
scanf("%s", str);
int p = 1;
for (int j = 0; str[j] != '\0'; j++) {
p = next[p][str[j] - 'a'];
}
printf("%d\n", count[p]);
}
}
}


Not AC代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: