zoukankan      html  css  js  c++  java
  • Hat’s Words

    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. 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. Output Your output should contain all the hat’s words, one per line, in alphabetical order. Sample Input a ahat hat hatword hziee word Sample Output ahat hatword #include #include #include #include char a[50001][50]; struct tire { int n; struct tire *next[26]; } *root; void jianshu(char a[50]) { tire *p1,*p2; p1=root; int n,i,id; n=strlen(a); for(i=0; i<n; i++) { id=a[i]-'a'; if(p1->next[id]==NULL) { p2=(tire*)malloc(sizeof(tire)); for(int j=0; j<26; j++) p2->next[j]=NULL; p2->n=0; p1->next[id]=p2; p1=p2; } else p1=p1->next[id]; } p1->n=1; } int find1(char b[50]) { tire *p1; p1=root; int n,i,id; n=strlen(b); for(i=0; i<n; i++) { id=b[i]-'a'; if(p1==NULL||p1->next[id]==NULL)return 0; p1=p1->next[id]; } return p1->n; } void xiao(tire *p1) { if(p1==NULL)return ; int i; for(i=0; i<26; i++) if(p1->next[i]!=NULL)xiao(p1->next[i]); free(p1); } int main() { int i=0,n; root=(tire*)malloc(sizeof(tire)); for(i=0; i<26; i++) root->next[i]=NULL; root->n=0; i=0; while(scanf("%s",a[i])!=EOF) jianshu(a[i]),i++; for(int j=0; j<i; j++) { n=strlen(a[j]); for(int k=1; k<n; k++) { char b[50]= {''},c[50]= {''}; strncpy(b,a[j],k); strncpy(c,a[j]+k,n-k); if(find1(b)&&find1(c)) { printf("%s ",a[j]); break; } } } xiao(root); }

  • 相关阅读:
    第一章 监控系统入门
    第十八章 Tagging_Redis6.2.1 服务器部署
    第十五章 ACL安全策略
    第十七章 Redis常见管理命令
    第五章 监控系统zabbix快速监控一台主机
    第十九章 Redis6.2.1脚本安装
    第三章 监控系统zabbix4.2官方源安装
    第十六章 Redis6.0其他新功能
    CSS教程:div垂直居中的N种方法
    css 文本两端对齐
  • 原文地址:https://www.cnblogs.com/woyaocheng/p/4708607.html
Copyright © 2011-2022 走看看