HDU1247 字典树

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 ,比较简单的字典树。

  刚学字典树不久,多做题练练手。

 


 

解法:

  先输入所有的字符串,建树。然后对所有的字符串进行枚举,将该字符串的前i位与后len-i位分为两个字符串,如果这两个字符串都在树中,则输出该字符串。由于题中给的数据是按照字典序的,所以不用考虑这点。

  这里用到了strncpy()函数——strncpy 是 C语言的库函数之一,来自 C语言标准库,定义于 string.h,char *strncpy(char *dest, char *src, int n),把src所指字符串的前n个字节复制到dest所指的数组中,并返回指向dest的指针。(from 百度百科)

 

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL __int64
const int maxn = 50000 + 5;
const int sigma_size = 26;
struct Trie {
    int ch[maxn][sigma_size];
    bool isEnd[maxn];
    int size;
    void init() {
        size = 1;
        memset(ch[0] , 0 , sizeof(ch[0]));
        memset(isEnd , 0 , sizeof(isEnd));
    }
    int index(char c) {    return c - 'a';    }
    void insert(char *s) {
        int i , rt;
        for(i = rt = 0 ; s[i] != '\0' ; i++) {
            int c = index(s[i]);
            if(!ch[rt][c]) {
                memset(ch[size] , 0 , sizeof(ch[size]));
                ch[rt][c] = size++;
            }
            rt = ch[rt][c];
        }
        isEnd[rt] = 1;
    }
    bool find(char *s) {
        int i , rt;
        for(i = rt = 0 ; s[i] != '\0' ; i++) {
            int c = index(s[i]);
            if(!ch[rt][c])    
                return false;
            rt = ch[rt][c];
        }
        return isEnd[rt];
    }
} trie;
char s[maxn][50];
char str[50];
int main() 
{
    int n;
    trie.init();
    for(n = 0 ; scanf("%s" , s[n]) != EOF ; n++) {
        trie.insert(s[n]);
    }
    for(int i = 0 ; i <= n ; i++) {
        int len = strlen(s[i]);
        for(int j = 1 ; j < len ; j++) {
            memset(str , 0 , sizeof(str));
            strncpy(str , s[i] , j);
            if(trie.find(str) && trie.find(s[i] + j)) {
                printf("%s\n" , s[i]);
                break;
            }
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/H-Vking/p/4471968.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值