Hat's Words hdu1247 trie

15 篇文章 0 订阅

Description


A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
You are to find all the hat’s words in a dictionary.

Solution


考虑用一颗trie记录所有出现过的单词,然后暴力拆分单词查询。长度实际只有100不会炸

Code


#include <stdio.h>
#include <string.h>
#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)
#define N 100001
#define L 105
int mp[N][26], T;
bool vis[N];
inline bool query(int now, int dep, char st[], int lim){
    if (dep == lim){
        return vis[now];
    }
    int tar = st[dep] - 'a';
    if (!mp[now][tar]){
        return 0;
    }
    return query(mp[now][tar], dep + 1, st, lim);
}
inline void ins(int now, int dep, char st[], int lim){
    if (dep == lim){
        vis[now] = 1;
        return;
    }
    int tar = st[dep] - 'a';
    if (!mp[now][tar]){
        mp[now][tar] = ++ T;
    }
    ins(mp[now][tar], dep + 1, st, lim);
}
char st[N][L];
int main(void){
    int n = 0;
//    scanf("%d", &n);
    T = 0;
    while (~scanf("%s", st[++ n])){
        int len = strlen(st[n]);
        ins(0, 0, st[n], len);
    }
    /*
    rep(i, 1, n){
        scanf("%s", st[i]);
        int len = strlen(st[i]);
        ins(0, 0, st[i], len);
    }
    */
    rep(i, 1, n){
        int len = strlen(st[i]);
        rep(j, 1, len - 1){
            int a = query(0, 0, st[i], j);
            int b = query(0, j, st[i], len);
            if (a & b){
                puts(st[i]);
                break;
            }
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值