HDU1247(经典字典树)

#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=50005;
const int N=26;
struct node{
    bool val;
    node* next[N];
};
node* root;
node memory[MAXN];
int ant;

node* create()
{
    node* p=&memory[ant++];
    for(int i=0;i<N;i++)
    {
        p->next[i]=NULL;
        p->val=false;
    }
    return p;
}

void insert(char *s)
{
    node* p=root;
    for(int i=0;s[i];i++)
    {
        int k=s[i]-‘a‘;
        if(p->next[k]==NULL) p->next[k]=create();
        p=p->next[k];
    }
    p->val=true;//若能走到最末端,则返回true;
}

bool search(char *s)
{
    node* p=root;
    for(int i=0;s[i];i++)
    {
        int k=s[i]-‘a‘;
        if(p->next[k]==NULL)    return false;
        p=p->next[k];
    }
    return p->val;//若只是某个已插入单词的前缀,则返回false;
}

int main()
{
    int cnt=0;
    root=create();
    char word[MAXN][20];
    while(scanf("%s",word[cnt])!=EOF)
    {
        //if(word[cnt][0]==‘0‘)    break;
        insert(word[cnt]);
        cnt++;
    }
    for(int i=0;i<cnt;i++)
    {
        for(int j=1;word[i][j];j++)
        {
            char fr[20]={‘\0‘};
            char re[20]={‘\0‘};
            strncpy(fr,word[i],j);
            strncpy(re,word[i]+j,strlen(word[i])-j);
            if(search(fr)&&search(re))
            {
                printf("%s\n",word[i]);
                break;
            }
        }
    }

    return 0;
}
时间: 2024-10-29 19:09:56

HDU1247(经典字典树)的相关文章

hdoj 1251 统计难题(经典字典树)

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 84414    Accepted Submission(s): 31834 Problem Description Contest time again! How excited it is to see balloons floating ar

HDU 4825 Xor Sum(经典01字典树+贪心)

Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 1555    Accepted Submission(s): 657 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Ze

hdu 1247:Hat’s Words(字典树,经典题)

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7282    Accepted Submission(s): 2639 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly

poj 2503:Babelfish(字典树,经典题,字典翻译)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have

hdu 1075:What Are You Talking About(字典树,经典题)

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 12617    Accepted Submission(s): 4031 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

HDU1247 字典树

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 ,比较简单的字典树. 刚学字典树不久,多做题练练手. 解法: 先输入所有的字符串,建树.然后对所有的字符串进行枚举,将该字符串的前i位与后len-i位分为两个字符串,如果这两个字符串都在树中,则输出该字符串.由于题中给的数据是按照字典序的,所以不用考虑这点. 这里用到了strncpy()函数——strncpy 是 C语言的库函数之一,来自 C语言标准库,定义于 string.h,char *s

hdu1247 Hat’s Words 字典树

Problem 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. Input Standard input consists of a number of lowercase words, on

HDU1247 Hat’s Words【字典树】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1247 题目大意: 一个"hat's word"是一个单词,可以恰好由字典中其他两个单词连接得到(比如字典中是hat's和word). 给出字典中的单词,输出所有的hat's word. 思路: 建立字典树,将每个单词都插入到Trie树中,Count统计单词(不是前缀)出现次数.按顺序将每个单词 所有可能的长度拆分成前缀单词和后缀单词,判断这两部分是否都在字典树中,是就是hat's wor

字典树经典问题

HDU4852 Xor Sum  字典树 题意 Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大,输出K(共有t组样例,(T < 10),N,M(<1=N,M<=100000)) 分析 将每个数插入到线段树后,对于每个s贪心的在字典树上走走即可