http://acm.hdu.edu.cn/showproblem.php?pid=1804
#include <stdio.h>
#include <string.h>
const int words_max_length = 23; //单词的最大长度,题目给的是20
struct node{
node * child[26];
char word[words_max_length];
node(){memset(child, 0, sizeof(child)); memset(word, 0, sizeof(word));}
}* root = new node(); // 先建立一个全局的根结点
void add_word(char * before, char * after)
{
//往字典树中加入数据,before是要检索的单词,after是最终结点中的数据,也就是对应不规则单词的复数形式
node * next = root;
while(*before)
{
if(next->child[*before-'a'] == NULL)
next->child[*before-'a'] = new node();
next = next->child[*before-'a'];
before++;
}
strcpy(next->word, after);
}
char * query(char * str)
{
//查询不规则复数单词表,返回的是单词的首地址,如果没有,返回空
node * next = root;
while(*str)
{
if(next->child[*str-'a'] == NULL)
return NULL;
next = next->child[*str-'a'];
str++;
}
return next->word;
}
int main()
{
//freopen("E:\\input.txt", "r", stdin);
int n, m;
scanf("%d %d", &n, &m);
char before[words_max_length], after[words_max_length];
while(n--)
{
scanf("%s %s", before, after);
add_word(before, after);
}
while(m--)
{
scanf("%s", before);
int len = strlen(before);
char * sptr = query(before);
//strcpy(after, query(before));
if(sptr != NULL)
printf("%s\n", sptr);
else if( (before[len-1] == 'y') &&
(before[len-2] != 'a' && before[len-2] != 'e' &&
before[len-2] != 'i' &&
before[len-2] != 'o' && before[len-2] != 'u') )
{
before[len-1] = 'i';
printf("%ses\n", before);
}
else if( (before[len-1]=='o') ||
(before[len-1]=='s') ||
(before[len-1]=='x') ||
(!strcmp(&before[len-2], "ch")) ||
(!strcmp(&before[len-2], "sh")) )
printf("%ses\n", before);
else
printf("%ss\n", before);
}
return 0;
}