hdu 1247

news/2024/7/7 20:14:45
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.

 

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
题解:刚开始就老想着在  find函数里修改,其实  模板不用变,就是多了对字符串的处理。
代码:
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <math.h>
  4 #include <algorithm>
  5 #include <iostream>
  6 #include <ctype.h>
  7 #include <iomanip>
  8 #include <queue>
  9 #include <stdlib.h>
 10 using namespace std;
 11 
 12 struct Tri
 13 {
 14     bool v;
 15     Tri* child[26];
 16 };
 17 
 18 Tri* root;
 19 
 20 void Init()
 21 {
 22     root->v=false;
 23     for(int i=0;i<26;i++)
 24     {
 25         root->child[i]=NULL;
 26     }
 27 }
 28 
 29 void CreateDic(char* s)
 30 {
 31     Tri* p;
 32     int j;
 33     int len=strlen(s);
 34     if(len==0)
 35         return ;
 36     p=root;
 37     for(int i=0 ;i < len; i++)
 38     {
 39         if(p->child[s[i]-'a']==NULL)
 40         {
 41             p->child[s[i]-'a']=(Tri*)new Tri;
 42             p->child[s[i]-'a']->v=false;
 43             for(j=0;j<26;j++)
 44                 p->child[s[i]-'a']->child[j]=NULL;
 45         }
 46         p=p->child[s[i]-'a'];
 47         
 48     }
 49     p->v=true;
 50 }
 51 
 52 bool Find(char *s)
 53 {
 54     Tri* p=root;
 55     int len=strlen(s);
 56     if(len==0)
 57         return 0;
 58     for(int i=0 ;i < len; i++)
 59     {
 60         if(p->child[s[i]-'a']==NULL)
 61             return 0;
 62         p=p->child[s[i]-'a'];
 63     }
 64     return p->v;
 65 }
 66 
 67 void Del(Tri* p)
 68 {
 69     for(int i=0;i<26;i++)
 70         if(p->child[i])
 71             Del(p->child[i]);
 72     Del(p);
 73     
 74 }
 75 
 76 char total[50002][100];
 77 int main()
 78 {
 79     char a[100],b[100],c[100];
 80     int i=0,j,k;
 81 
 82     root=(Tri*)new Tri;
 83     Init();
 84     while(gets(a))
 85     {
 86         CreateDic(a);
 87         strcpy(total[i++],a);
 88     }
 89 
 90     for(j=0;j<i;j++)
 91     {
 92         for(k=1;k<strlen(total[j]);k++)
 93         {
 94             strncpy(b,total[j],k);
 95             b[k]='\0';
 96             strcpy(c,total[j]+k);
 97                 
 98             if(Find(b)&&Find(c))
 99             {
100                 cout<<total[j]<<endl;
101                 break;   //注意结束循环                   
102             }
103         }
104     }
105     return 0;
106 }

 

转载于:https://www.cnblogs.com/wangmengmeng/p/5011313.html


http://lihuaxi.xjx100.cn/news/244070.html

相关文章

从虚拟化前端Bug学习分析Kernel Dump

前言 也许大家都知道&#xff0c;分析 Kernel Dump 有个常用的工具叫 Crash&#xff0c;在我刚开始学习分析 Kernel Dump 的时候&#xff0c;总是花大量的时间折腾这个工具的用法&#xff0c;却总是记不住这个工具的功能。后来有一次在参加某次内部分享的时候&#xff0c;有位大…

SpringBoot默认包扫描问题

SpringBootApplication注解默认扫描路径是&#xff1a; 自动扫描主程序所在包及其下面的所有子包里面的组件 在maven多模块项目中&#xff0c;如果想让扫描到&#xff0c;需要在子模块下面创建相同的包 如&#xff1a; 如果包名不同就需要使用ComponentScan注解来扫描 但是…

字母金字塔

s 65 h a 10 while a:s chr(s)h h sprint(h)s ord(s)s s 1a a - 1 a 26 s 65 h A u * (a 1) while a:print(u,h)s s 1s chr(s)h s h ss ord(s)u u[1:]a a - 1 转载于:https://www.cnblogs.com/wumac/p/5704035.html

IOS推送详解

为什么80%的码农都做不了架构师&#xff1f;>>> IOS推送详解 一.关于推送通知 推送通知&#xff0c;也被叫做远程通知&#xff0c;是在iOS 3.0以后被引入的功能。是当程序没有启动或不在前台运行时&#xff0c;告诉用户有新消息的一种途径&#xff0c;是从外部服务…

控制反转 IOC

2019独角兽企业重金招聘Python工程师标准>>> 控制反转&#xff08;Inversion of Control&#xff0c;缩写为IoC&#xff09;面向对象设计原则&#xff0c;降低代码耦合度 依赖注入&#xff08;Dependency Injection&#xff0c;简称DI&#xff09; 依赖查找&#xf…

若依配置多数据源

1.在application.yml配置新增的数据库 octmes:# 从数据源开关/默认关闭enabled: trueurl: jdbc:mysql://localhost:3306/octmes?useUnicodetrue&characterEncodingUTF-8username: rootpassword: root 2.在DataSourceType.java 设置数据库名称 /*** 主库*/MASTER,/*** 从…

13.类的关系总结

下面这张UML图(该图为网上找到的)&#xff0c;比较形象地展示了各种类图关系&#xff1a; 对于继承、实现这两种关系没多少疑问&#xff0c;它们体现的是一种类与类、或者类与接口间的纵向关系&#xff1b;其他的四者关系则体现的是类与类、或者类与接口间的引用、横向关系&…

ArrayList的内存泄露

2019独角兽企业重金招聘Python工程师标准>>> 大家先运行下下面这段代码&#xff0c;看看结果 public class MemoryLeak {public static void main(String[] args) throws InterruptedException {new Thread(new Runnable() {Overridepublic void run() {for (int i …