HDU1247Hat’s Words
来源:互联网 发布:php flarum 编辑:程序博客网 时间:2024/05/05 21:35
题目链接:HDU1247
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12193 Accepted Submission(s): 4350
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.
You are to find all the hat’s words in a dictionary.
Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
Only one case.
Only one case.
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
Sample Input
aahathathatwordhzieeword
Sample Output
ahathatword
题意:找出可以被拆成2个串的串,这两个串肯定要在给出串的范围内,还有就是要注意可以是同一个串。
题目分析:字典树的模板改一改就可以了,不过不知道为啥我写的程序就是无限WA,调了一整天最终无奈放弃,在网上照一个巨巨的代码修改一下,然后过了。。。
AC代码:
#include <iostream>#include <cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;typedef struct node { int flag; int buf; node *next[26];}trienode;trienode *root;char s[50015][100];void insert_node(char *s){ trienode *t,*h=root; int len=strlen(s); for(int i=0;i<len;i++) { int buf=s[i]-'a'; if(h->next[buf]==NULL) { t=new node; for(int j=0;j<26;j++) t->next[j]=NULL; t->flag=0; h->next[buf]=t; h=t; } else h=h->next[buf]; } h->flag=1;}void del(trienode *r){ for(int i=0;i<26;i++) { if(r->next[i]) del(r->next[i]); } free(r);}int find(char *t){ trienode *r=root; int len1=strlen(t); int l=0; for(l=0;l<len1;l++) { int buf1=t[l]-'a'; if(r->next[buf1]!=NULL) { r=r->next[buf1]; } else return 0; } return r->flag;}int main(){ char t[100]; root=new node; for(int i=0;i<26;i++) root->next[i]=NULL; root->flag=0; int k=0; while(~scanf("%s",t)) { strcpy(s[k++],t); insert_node(t); } for(int i=0;i<k;i++) { int len=strlen(s[i]); int j=1; //int flag1=0; if(strcmp(s[i],s[i+1])==0) continue; for(j=1;j<len;j++) { char t1[100],t2[100]; strcpy(t1,s[i]); t1[j]='\0'; strcpy(t2,s[i]+j); if(find(t1)&&find(t2)) { printf("%s\n",s[i]); break; } } } del(root);}
这是无限WA的代码。。。。还请大家帮我找找到底哪出问题了,卡在31msWA了。
//// main.cpp// HDU1247a//// Created by teddywang on 16/3/29.// Copyright © 2016年 teddywang. All rights reserved.//#include <iostream>#include <cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;typedef struct node { int flag; int buf; node *next[26];}trienode;trienode *root;char s[50015][100];void insert_node(char *s){ trienode *t,*h=root; int len=strlen(s); for(int i=0;i<len;i++) { int buf=s[i]-'a'; if(h->next[buf]==NULL) { t=new node; for(int j=0;j<26;j++) t->next[j]=NULL; t->flag=0; h->next[buf]=t; } h=h->next[buf]; } h->flag=1;}void del(trienode *r){ for(int i=0;i<26;i++) { if(r->next[i]) del(r->next[i]); } free(r);}int main(){ root=new node; for(int i=0;i<26;i++) root->next[i]=NULL; int k=0; while(~scanf("%s",s[k])) { insert_node(s[k++]); } for(int i=0;i<k;i++) { trienode *h=root; int len=strlen(s[i]); int j=0; // if(strcmp(s[i],s[i+1])==0) continue;不知道该不该加 for(j=0;j<len;j++) { int buf=s[i][j]-'a'; if(h->next[buf]!=NULL&&h->flag==0) { h=h->next[buf]; } else { if(j==len) break; h=h->next[buf]; int flag=0; trienode *r=root; int len1=len-j; int l=0; for(l=0;l<len1;l++) { int buf1=s[i][j+l]-'a'; if(r->next[buf1]!=NULL) { r=r->next[buf1]; } else break; } if(l==len1) { if(r->flag==1) flag=1; else flag=0; } if(flag==1) { printf("%s\n",s[i]); break; } } } } del(root);}
0 0
- hdu1247Hat’s Words
- HDU1247Hat’s Words--Trie
- hdu1247Hat’s Words
- hdu1247Hat’s Words
- HDU1247Hat’s Words
- 【字典树】HDU1247Hat’s Words
- hdu1247Hat’s Words (组合单词,字典树+DFS)
- hdu1247Hat’s Words(字典树---判断单词有无组合现象)
- Interpreter's Words
- Mecool's words
- Jane's words
- HDOJ Hat’s Words
- Hat’s Words
- Hat’s Words
- Hat’s Words
- hdu1247 Hat’s Words
- Hat’s Words
- Hat’s Words
- PRML 学习笔记(1)
- c++上机作业测试体重是否标准并输出相关提示
- Yii2 选择布局的方式 [ 2.0 版本 ] layout
- 使用 Pascal 脚本编写网页, PWP 项目
- history pushState 实现浏览器前进与后退
- HDU1247Hat’s Words
- 微信的一些优点整理(持续更新)
- Java并发:并发编程带来的好处和风险
- 如何修改myeclipse的内存?eclipse.ini中各个参数的作用
- Android activity生命周期的几种场景
- Light oj1422 Halloween Costumes 区间DP
- dat多重调用bat与powershell
- 贪心算法7之1002
- MFC中快速应用OpenCV