字典树裸题,每次插入完毕后,把这个字符串对应的串存到当前结点里。
查询的时候即可。。再特殊处理一下没在字典树里的其他情况,y前是辅音才转换为ies。。
因为这个WA了一次。。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
struct Trie
{
Trie *next[26];
bool flag; // 标记字符串结尾结尾
char *st; // 插入操作结束后,把对应的字符串放进去
};
Trie *H;
Trie *newnode()
{
Trie *p = new Trie;
for(int i = 0; i < 26; i++)
{
p->next[i] = NULL;
}
p->flag = false;
p->st = NULL;
}
int idx(char c)
{
return c - 'a';
}
void insert(char *ss, char *x)
{
Trie *p = H;
int len = strlen(ss);
for(int i = 0; i < len; i++)
{
int c = idx(ss[i]);
if(p->next[c] == NULL)
{
p->next[c] = newnode();
}
p = p->next[c];
}
p->flag = true;
p->st = new char[21];
strcpy(p->st, x);
}
int query(char *ss)
{
Trie *p = H;
int len = strlen(ss);
for(int i = 0; i < len; i++)
{
int c = idx(ss[i]);
if(p->next[c] == NULL)
{
return 0;
}
p = p->next[c];
}
if(p->flag)
{
puts(p->st);
return 1;
}
return 0;
}
int main()
{
//freopen("test0.in", "r", stdin);
//freopen("test0.out", "w", stdout);
//srand(time(NULL));
char flag[5][10]={"o", "s", "ch", "sh", "x"}; // 以这些结尾变es
int vowel[26]={0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1}; // 元音、辅音标记数组
char *ss; //查找某个字符串的子串指针
int L, N;
char ch1[100], ch2[100], ch3[100];
while(~scanf("%d %d", &L, &N))
{
H = newnode();
for(int i = 0; i < L; i++)
{
scanf("%s %s", ch1, ch2);
insert(ch1, ch2);
}
for(int i = 0; i < N; i++)
{
scanf("%s", ch3);
int len = strlen(ch3);
int flg = 0;
if(!query(ch3))
{
for(int j = 0; j < 5; j++)
{
ss = ch3;
if(len < strlen(flag[j]))
{
continue;
}
while(ss = strstr(ss, flag[j]))
{
if(ss - ch3 == len - strlen(flag[j]))
{
flg = 1;
strcat(ch3, "es");
break;
}
ss++;
}
}
if(!flg)
{
if(ch3[len-1] == 'y' && len-2 >= 0 && vowel[ch3[len-2]])
{
ch3[len-1] = 'i';
strcat(ch3, "es");
}
else
{
strcat(ch3, "s");
}
}
printf("%s\n", ch3);
}
}
}
return 0;
}