hdu 1247 Hat’s Words

/*字典树!

这道题还真是坑爹啊!

要查询是不是整个词是不是存在,只要判断最后一位是不是有记录!*/

#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <algorithm> #include <vector> #include <limits.h> #include <queue> #include <stack> using namespace std; struct trie { int cnt; trie* next[26]; }; char str[50010][50]; void inst(trie *root,char *s) { int i; trie* p = root; i = 0; while(s[i]!='\0') { int k = s[i]-'a'; if(p->next[k]==NULL) { trie *tmp = new trie; for(int j = 0;j < 26;j ++) tmp->next[j] = NULL; tmp->cnt = 0; p->next[k] = tmp; } //else p->cnt ++; p = p->next[k]; i ++; } p->cnt = 1; } bool search(trie* root,char *s) { int i = 0; bool f = 1; trie* p = root; //cout<<s<<endl; while(s[i]!='\0') { int k = s[i]-'a'; if(p->next[k]==NULL) return 0; else p = p->next[k]; i ++; } if(p->cnt == 1) return 1; else return 0; } int main() { int i=0; trie *root = new trie; for(i = 0;i < 26;i ++) root->next[i] = NULL; root ->cnt = 0; i = 0; while(scanf("%s",str[i])!=EOF) { inst(root,str[i]); i ++; //if(i > 5 )break; } //sort(str,str+i,cmp2); for(int j = 0;j < i;j ++) { for(int l = 0;l < strlen(str[j]);l ++) { char temp1[50]={'\0'}; char temp2[50]={'\0'}; strncpy(temp1,str[j],l); strncpy(temp2,str[j]+l,strlen(str[j])-l); //cout<<temp1<<" "<<temp2<<endl; if(search(root,temp1)&&search(root,temp2)) { printf("%s\n",str[j]); break; } } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值