UVA10391_Compound Words/HDU1247_Hat's Words(字典树)
来源:互联网 发布:淘宝鹊桥入口在哪里 编辑:程序博客网 时间:2024/05/21 14:46
解题报告
两题一模一样,连代码都一样。
用了+才 的思路,很神奇的思路,不过仅限解此题。。。
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;struct node{ int v; node *next[26];};node *newnode(){ node *p=new node; p->v=0; for(int i=0; i<26; i++) { p->next[i]=NULL; } return p;}void insertnode(node *root,char *str){ node *p=root; int l=strlen(str); for(int i=0; i<l; i++) { int t=str[i]-'a'; if(p->next[t]==NULL) { p->next[t]=newnode(); } p=p->next[t]; } p->v=1;}void find(node *root,char *str){ int k=0,f=0; int i,j; node *p=root; int l=strlen(str); for(i=0; i<l; i++) { int t=str[i]-'a'; if(p->next[t]==NULL) return ; p=p->next[t]; if(p->v==1)//核心代码 { node *q=root; for(j=i+1; j<l; j++) { int u=str[j]-'a'; if(q->next[u]==NULL) break ; q=q->next[u]; if(q->v==1) { if(str[j+1]=='\0') { printf("%s\n",str); return; } else continue ; } } } }}char ch[200000][1000];int main(){ char s[10000]; node *root=newnode(); int i=0; while(scanf("%s",ch[i])!=EOF) { insertnode(root,ch[i]); i++; } for(int j=0; j<i; j++) { find(root,ch[j]); } return 0;}
还有一个类似暴力的解法。。。
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct node{ int v; node *next[26];} T[1000000];int t=0;node *newnode(){ node *p=new node;//动态分配 //node *p=&T[t++];//静态分配 p->v=0; for(int i=0; i<26; i++) p->next[i]=NULL; return p;}void insertnode(node *root,char *str)//插入{ node *p=root; int l=strlen(str); for(int i=0; i<l; i++) { int t=str[i]-'a'; if(p->next[t]==NULL) p->next[t]=newnode(); p=p->next[t]; } p->v=1;}int find(node *root,char *str)//查找{ node *p=root; int l=strlen(str); for(int i=0; i<l; i++) { int t=str[i]-'a'; if(p->next[t]==NULL) return 0; p=p->next[t]; } return p->v;}void freenode(node *root)//释放内存{ node *p=root; for(int i=0; i<26; i++) if(p->next[i]!=NULL) freenode(p->next[i]); free(p);}char ch[240000][100];char sl[240],sr[240];int main(){ int i=0; node *root=newnode(); while(~scanf("%s",ch[i])) { insertnode(root,ch[i]); i++; } for(int j=0; j<i; j++) for(int k=1; k<strlen(ch[j]); k++) { memset(sl,0,sizeof(sl)); memset(sr,0,sizeof(sr)); strncpy(sl,ch[j],k); strcpy(sr,ch[j]+k); int nl=find(root,sl); int nr=find(root,sr); if(nl&&nr) { printf("%s\n",ch[j]); break; } } return 0;}
0 0
- UVA10391_Compound Words/HDU1247_Hat's Words(字典树)
- hdu Hat‘s Words(字典树)
- HDU_1247_Hat’s Words(字典树)
- Hat’s Words(字典树)
- HD_1247Hat’s Words (字典树)
- HDU1247 Hat’s Words (字典树)
- hdu1247 Hat’s Words(字典树)
- HDU1247:Hat’s Words(字典树)
- Hat‘s Words(字典树)
- (字典树)Hat’s Words -- HDOJ
- Hat’s Words(字典树)
- Hat’s Words(字典树)
- 【字典树】HDU1247Hat’s Words
- Hat’s Words hdu 1247(字典树 map)
- HDU 1247:Hat’s Words(字典树)
- hdu 1247 Hat’s Words(字典树)
- hdu 题目1247 Hat’s Words(字典树)
- HDU 3530 Hat’s Words(字典树)
- 【题解】[wikioi2873]日程表安排
- Well begun is half done!
- 20140218-lua binder另一只轮子的雏形(2014-01-02 20:08)
- 由Bug延伸的一点有关“兼容与重构”思考
- 苹果移动设备唯一标识的变化
- UVA10391_Compound Words/HDU1247_Hat's Words(字典树)
- 向前端迈进
- Linux下端口被占用解决
- zoj 3160(区间dp 简单)
- linux+oracle 11g 集群的安装
- 寒假训练--dfs+bfs--找朋友
- 守护进程
- 【ios游戏开发】之UDID和OpenUDID扫盲
- 面向对象编程风格 VS 基于对象编程风格(boost::bind/function)