HDOJ 1247 Hat’s Words (字典树分割单词枚举切割点)
来源:互联网 发布:俄罗斯生活知乎 编辑:程序博客网 时间:2024/06/10 04:24
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11409 Accepted Submission(s): 4075
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题意:给你一堆单词,让你求是否其中一个单词分割为两个单词而且这两个单词都在这几个单词中,例如:ahat分割为a和hat思路:把所有的单词都输入后,一个个暴力枚举切割点,然后查看两段单词是否存在,这个输入不知道怎么回事,所以就一次又一次的试ac代码:#include<stdio.h>#include<string.h>#include<math.h>#include<stack>#include<iostream>#include<algorithm>#define fab(a) (a)>0?(a):(-a)#define LL long long#define MAXN 55000#define mem(x) memset(x,0,sizeof(x))#define INF 0xfffffff using namespace std;struct s{int num;//标记单词的存在性s *next[26];};s *root;char ss[MAXN][255];int v[MAXN];void create(char *str){int len=strlen(str);s *p=root,*q;for(int i=0;i<len;i++){int id=str[i]-'a';if(p->next[id]==NULL){q=(s *)malloc(sizeof(s));for(int j=0;j<26;j++)q->next[j]=NULL;q->num=0;p->next[id]=q;p=p->next[id];}else{p=p->next[id];}}p->num=1;}int find(char *ss){int len=strlen(ss);s *p=root;for(int i=0;i<len;i++){int id=ss[i]-'a';p=p->next[id];if(p==NULL)return 0;//没找到}return p->num;//找到了}void begin(){for(int i=0;i<26;i++)root->next[i]=NULL;root->num=0;}void freetree(s *t){if(t==NULL)return;for(int i=0;i<26;i++){if(t->next[i]!=NULL)freetree(t->next[i]);}free(t);return;}int main(){root=(s *)malloc(sizeof(s));begin();int k=0;char ne1[260],ne2[260];while(scanf("%s",ss[k])!=-1){create(ss[k]);k++;}for(int i=0;i<k;i++){int len=strlen(ss[i]);v[i]=0;if(len==1)continue;for(int j=1;j<len;j++){ int qq=0;for(int q=0;q<j;q++)ne1[qq++]=ss[i][q];ne1[qq]='\0';qq=0;for(int q=j;q<len;q++)ne2[qq++]=ss[i][q];ne2[qq]='\0';if(find(ne1)&&find(ne2))//两个都能找到,标记{v[i]=1;break;}}}for(int i=0;i<k;i++)if(v[i])printf("%s\n",ss[i]);return 0;}
0 0
- HDOJ 1247 Hat’s Words (字典树分割单词枚举切割点)
- [HDOJ 1247] Hat’s Words [字典树]
- 字典树-HDOJ-1247-Hat’s Words
- hdoj-1247-Hat’s Words-字典树
- hdu 1247 Hat’s Words 字典树统计单词
- hdu 1247 Hat’s Words 字典树单词拼接
- (字典树)Hat’s Words -- HDOJ
- HDOJ Hat’s Words 1247【经典字典树】
- HDOJ 1247 Hat’s Words(字典树变形)
- HDU 1247 Hat’s Words(字典树)
- hdu 1247 字典树 Hat’s Words
- HDU 1247 Hat’s Words 字典树
- hdu 1247 Hat’s Words 字典树
- HDU 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words字典树
- hdu 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words 字典树
- 类和对象
- R语言学习路线和常用数据挖掘包
- - (void)layoutSublayersOfLayer:(CALayer *)layer
- viewpager、listview、gridview、scrollview去除阴影
- 线程的等待唤醒机制
- HDOJ 1247 Hat’s Words (字典树分割单词枚举切割点)
- Spark机器学习3
- Android Context 是什么?
- nginx报 File not found 错误
- 使用VMware VSphere WebService SDK进行开发 (七)——获取数据中心、集群、主机、虚拟机的目录结构
- 【leetcode】Kth Smallest Element in a BST
- UVa 914 - Jumping Champion
- A3 NO SELECT The login is O EXAMINE The login is not saf your mail client: http://mail.163.com/dashi
- Zookeeper已经分布式环境中的假死脑裂