HDU 1075 What Are You Talking About

HDU_1075

    为了练习一下字典树所以做了一下这个题目。由于题目数据比较多,数组开小了会WA。

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define MAXD 500010
char str[30], b[30], txt[3010], word[MAXD][12];
int flag[MAXD], p[MAXD], next[MAXD][26], w, e;
void insert(int cur, int k)
{
++ e;
flag[e] = 0;
memset(next[e], 0, sizeof(next[e]));
next[cur][k] = e;
}
void init()
{
int i, j, k, cur;
w = e = 0;
memset(next[e], 0, sizeof(next[e]));
gets(b);
for(;;)
{
gets(b);
if(strcmp("END", b) == 0)
break;
sscanf(b, "%s%s", word[++ w], str);
cur = 0;
for(i = 0; str[i]; i ++)
{
k = str[i] - 'a';
if(!next[cur][k])
insert(cur, k);
cur = next[cur][k];
}
flag[cur] = 1;
p[cur] = w;
}
}
int find()
{
int i, j, k, cur;
cur = 0;
for(i = 0; str[i]; i ++)
{
k = str[i] - 'a';
if(!next[cur][k])
return 0;
cur = next[cur][k];
}
if(!flag[cur])
return 0;
printf("%s", word[p[cur]]);
return 1;
}
void solve()
{
int i, j, k;
gets(b);
for(;;)
{
gets(txt);
if(strcmp(txt, "END") == 0)
break;
for(i = 0; txt[i]; i ++)
{
if(isalpha(txt[i]))
{
for(j = 0; isalpha(txt[i]); j ++, i ++)
str[j] = txt[i];
str[j] = '\0';
if(!find())
printf("%s", str);
-- i;
}
else
printf("%c", txt[i]);
}
printf("\n");
}
}
int main()
{
init();
solve();
return 0;
}


你可能感兴趣的:(HDU)