您的位置:首页 > 其它

《算法竞赛-训练指南》-第三章-Trie

2013-09-09 17:25 267 查看
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>

using namespace std;

const int MAXNODE = 1000000 + 11;

const int SEGMA_SIZE = 26;

struct Trie
{
int ch[MAXNODE][SEGMA_SIZE];
int val[MAXNODE];
int size;
Trie()
{
size = 1;
memset(ch[0], 0, sizeof(ch[0]));
}
void clear()
{
size = 1;
memset(ch[0], 0, sizeof(ch[0]));
}
int idx(char c) // 求出字符所对应的id
{
return c - 'a';
}
void insert(char *s, int v)
{
int n = strlen(s);
int u = 0;
for (int i = 0; i < n; i++)
{
int id = idx(s[i]);
if (!ch[u][id])
{
memset(ch[size], 0, sizeof(ch[size]));
val[size] = 0;
ch[u][id] = size++;
}
u = ch[u][id];
}
val[u] = v;
}
int search(char *s)
{
int n = strlen(s);
int u = 0;
for (int i = 0; i < n; i++)
{
int id = idx(s[i]);
if (!ch[u][id])
{
return 0;
}
u = ch[u][id];
}
if (val[u] == 1)
{
return 1;
}
}

}trie;

int main()
{
char str[111];
int N, M;
while (scanf("%d%d", &N, &M) != EOF)
{
trie.clear();
for (int i = 0; i < N; i++)
{
scanf("%s", str);
trie.insert(str, 1);
}
for (int i = 0; i < M ; i++)
{
scanf("%s", str);
int ans = trie.search(str);
if (ans == 1)
{
printf("The string exists\n");
}
else
{
printf("The string doesn`t exist\n");
}
}
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: