[字典树] HDU 1804 - Deli Deli

字典树裸题,每次插入完毕后,把这个字符串对应的串存到当前结点里。

查询的时候即可。。再特殊处理一下没在字典树里的其他情况,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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值