hdu1247 Hat’s Words (字典树)
来源:互联网 发布:全能数据人生txt下载 编辑:程序博客网 时间:2024/05/21 14:59
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247
题解:给一些单词,找出那些可以分成另外的两个单词的单词,字典树
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 50002typedef struct node { struct node *child[26];//存储下一个字符 int cnt;//该节点是否是单词的结尾}node,*Trie; Trie root;//字典树的根节点 char words[MAXN][16];void insert(char *str) { int i,j,index,len; Trie current,newnode; len=strlen(str); if(len==0) return;//单词长度为0 current=root;//当前节点为根节点 for(i=0;i<len;++i) { index=str[i]-'a'; if(current->child[index]!=NULL)//字典树存在该字符 { current=current->child[index]; } else { newnode=(struct node *)malloc(sizeof(struct node));//新增一节点 for(j=0;j<26;++j) newnode->child[j]=NULL; newnode->cnt=0; current->child[index]=newnode; current=newnode;//修改当前节点 } } current->cnt=1;//该节点是单词的尾} int query(char *str) { int i=0,index,len,ans; int top=0,stack[1000];Trie current; len=strlen(str); if(len==0) return 0; current=root;//从根节点开始查找 while(str[i]) { index=str[i++]-'a'; if(current->child[index]==NULL)return 0;//字典树不存在此单词 current=current->child[index]; if(current->cnt&&str[i])//找到该单词含有子单词的分隔点 stack[top++]=i;} while(top){ans=1;i=stack[--top];current=root;while(str[i]){index=str[i++]-'a';if(current->child[index]==NULL){ans=0;break;}current=current->child[index];}if(ans&¤t->cnt)return 1;}return 0;} void Del(Trie root)//释放内存 { int i; if(root==NULL) return; for(i=0;i<26;++i) { if(root->child[i]!=NULL) Del(root->child[i]); } free(root); root=NULL; } int main() { int i,cnt=0; root=(struct node *)malloc(sizeof(struct node)); for(i=0;i<26;++i) root->child[i]=NULL; root->cnt=0; while(scanf("%s",words[cnt])!=EOF){insert(words[cnt]);cnt++;}for(i=0;i<cnt;++i){if(query(words[i]))printf("%s\n",words[i]);}Del(root); return 0; }
- 【字典树】 hdu1247 Hat’s Words
- Hat’s Words hdu1247 字典树+搜索
- HDU1247-Hat’s Words(字典树)
- Hat’s Words(hdu1247字典树)
- hdu1247 Hat’s Words (字典树)
- hdu1247 Hat’s Words(字典树)
- Hdu1247 - Hat’s Words - 字典树
- hdu1247 Hat’s Words 字典树
- HDU1247 Hat’s Words【字典树】
- HDU1247 Hat’s Words (字典树)
- HDU1247 Hat’s Words (字典树)
- hdu1247 Hat’s Words(字典树)
- hdu1247 Hat’s Words ---- 字典树
- hdu1247 Hat’s Words (字典树)
- hdu1247 hat's words 字典树
- HDU1247:Hat’s Words(字典树)
- hdu1247 Hat’s Words(字典树:模板+枚举)
- HDU1247 Hat’s Words 解题报告--字典树
- 内存映射文件详解-----C++实现
- dm365管脚复用配备浅析之davinci_cfg_reg调用 .
- [5] Zygote
- UIFont ios自体设置
- AndroidManifest文件详解(uses-sdk)
- hdu1247 Hat’s Words (字典树)
- Java Multi-thread Programming
- POJ1228(稳定凸包问题)
- 1022——挂盐水
- DM6446开发攻略——u-boot-1.3.4移植(1) .
- 51上写操作系统1(三任务调度)
- select option的使用
- linux i2c驱动架构-dm368 i2c驱动分析 .
- 黑马程序员--JAVA基础案例之交通灯管理系统