Hat’s Words

Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13798    Accepted Submission(s): 4950

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

Author

戴帽子的

Recommend

Ignatius.L   |   We have carefully selected several similar problems for you:  1075 1298 1800 2846 1305

/*
一开始想到将单词分割,但是怕超时,后来一想单词长度可能10^2差不多,10^7的运行时间应该不会超时
*/
#include<bits/stdc++.h>
#define N 30
using namespace std;
#define MAX 26
const int maxnode=4000*100+100;///预计字典树最大节点数目
const int sigma_size=26;///每个节点的最多儿子数

struct Trie
{
    ///这里ch用vector<26元素的数组> ch;实现的话,可以做到动态内存
    int ch[maxnode][sigma_size];///ch[i][j]==k表示第i个节点的第j个儿子是节点k
    int val[maxnode];///val[i]==x表示第i个节点的权值为x
    int sz;///字典树一共有sz个节点,从0到sz-1标号

    ///初始化
    void Clear()
    {
        sz=1;
        memset(ch[0],0,sizeof(ch[0]));///ch值为0表示没有儿子
    }

    ///返回字符c应该对应的儿子编号
    int idx(char c)
    {
        return c-‘a‘;
    }

    ///在字典树中插入单词s,但是如果已经存在s单词会重复插入且覆盖权值
    ///所以执行Insert前需要判断一下是否已经存在s单词了
    void Insert(char *s)
    {
        int u=0,n=strlen(s);
        for(int i=0;i<n;i++)
        {
            int id=idx(s[i]);
            if(ch[u][id]==0)///无该儿子
            {
                ch[u][id]=sz;
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz++]=0;
            }
            u=ch[u][id];
        }
        val[u]=n;
    }

    ///在字典树中查找单词s
    bool Search(char *s)
    {
        int n=strlen(s),u=0;
        for(int i=0;i<n;i++)
        {
            int id=idx(s[i]);
            if(ch[u][id]==0)
                return false;
            u=ch[u][id];
        }
        return val[u];
    }
};
Trie trie;///定义一个字典树
char op[50005][N];
int main()
{
    //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
    int len=0;
    trie.Clear();
    while(gets(op[len++]))
        trie.Insert(op[len-1]);
    //cout<<len<<endl;
    for(int i=0;i<len-1;i++)
    {
        int n=strlen(op[i]);
        //cout<<"op[i]="<<op[i]<<" i="<<i<<endl;
        for(int k=1;k<n;k++)
        {
            char str1[N],str2[N];
            strncpy(str1,op[i],k);
            str1[k]=‘\0‘;
            strncpy(str2,op[i]+k,n);
            str2[n-k]=‘\0‘;
            if(trie.Search(str1)&&trie.Search(str2))
            {
                puts(op[i]);
                break;//这一句是必须加的因为不加就会输出两遍
            }
            //cout<<str1<<" "<<str2<<endl;
        }
        //puts(op[i]);
    }
    return 0;
}
时间: 2024-08-27 09:35:06

Hat’s Words的相关文章

Linux red hat 安装ansible

今日对Linux 系统是Red Hat Enterprise Linux Server release 6.5 (Santiago)对ansible进行安装. 由于系统的源为yum源,所以使用yum install ansible 进行安装,但是报错.如图.(这个错误是yum源没有注册到red hat 系统). yum源不能安装,所以换了一个思路.使用pip安装.pip是依赖python安装的. 1.检查Python版本 Python -v 检查出来为Python 2.6.6 2.检查pip 版

在VMware中为Redhat HAT配置本地yum源

在VMware中为Redhat HAT配置本地yum源 今天准备使用CM安装大数据环境,到需要几台机器都使用同一套yum源才可以,所以想到将Redhat镜像文件拷贝到虚拟机中,在挂起使用,最后通过http分享给其他虚拟机.后再想想,这样做真的很愚蠢,首先在虚拟机安装的时候我已经挂起过一次镜像啦,所以我只要在每个虚拟机中再挂起一下,直接配置yum源就可以,也不用将iso拷贝到虚拟机中,减少磁盘占用.所以现在就来说说怎么做: 一.安装前检查,首先需要确保你的虚拟机已经加载了本地镜像,如下图所示: 注

hdu 1247 Hat’s Words(map)

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8527    Accepted Submission(s): 3069 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactl

Linux(Red Hat 6 32位) 下安装Mysql5.6.30

1. 下载MySQL 5.6 下载页面:http://dev.mysql.com/downloads/mysql/ 此处选择"Red Hat Enterprise Linux 6 / Oracle Linux 6 (x86, 32-bit), RPM Bundle"下载,下载至/root/fuxian/目录下,下载文件名为"MySQL-5.6.30-1.el6.i686.rpm-bundle.tar" 2. 解压tar包 cd /fuxian/Downloads/

System center 2012 R2 实战七、SCOM2012R2监控Red hat Linux5.7

1    准备工具 u  putty_V0.63.0.0.43510830.exe ------>用于连接到Linux服务器 u  Winscp  ------>用于将windows与 Linux服务器文件互传 2    操作步骤 复制代理至Linux服务器 在SCOM安装光盘中,找到Linux服务器代理 打开Wincap工具,将用于Linux服务器的SCOM代理,复制到Linux服务器根目录下 1            域名解析配置 u  修改Linux服务器主机名称+DNS后缀 u  去D

Red Hat Enterprise Linux 5.10在vmware10下的安装

Red Hat Enterprise Linux 5.10在vmware10下的安装 1.启动"新建虚拟机"向导程序.如下图,选择"自定义",点击"下一步" 2.选择虚拟机硬件兼容性,你可以根据自己需求选择,这里选择默认,如下图.单击"下一步" 3.指定虚拟机系统的安装方式.选择"稍后安装操作系统",如果选择"安装盘镜像文件(iso)",虚拟机启动后会自动执行快速安装,不方便用户控制安装

Configure Red Hat Enterprise Linux shared disk cluster for SQL Server

下面一步一步介绍一下如何在Red Hat Enterprise Linux系统上为SQL Server配置共享磁盘集群(Shared Disk Cluster)及其相关使用(仅供测试学习之用,基础篇) 一.      创建共享磁盘和 Cluster 微软官方配置文档:https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-shared-disk-cluster-red-hat-7-configure. Linux Cluster结构

SQL Server on Red Hat Enterprise Linux

本文从零开始一步一步介绍如何在Red Hat Enterprise Linux上搭建SQL Server 2017,包括安装系统.安装SQL等相关步骤和方法(仅供测试学习之用,基础篇). 一.   创建RHEL系统(Create Red Hat Enterprise Linux System) 1.      前提准备 由于本文主要研究SQL Server 2017在Linux上的搭建方法,从Install SQL Server on Linux中得知当前SQL Server 2017 CTP

Configure Red Hat Enterprise Linux shared disk cluster for SQL Server——RHEL上的“类”SQL Server Cluster功能

下面一步一步介绍一下如何在Red Hat Enterprise Linux系统上为SQL Server配置共享磁盘集群(Shared Disk Cluster)及其相关使用(仅供测试学习之用,基础篇) 一.      创建共享磁盘和 Cluster 微软官方配置文档:https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-shared-disk-cluster-red-hat-7-configure. Linux Cluster结构

yum报错:This system is not registered to Red Hat Subscription Management.解决办法

使用yum安装软件,报错如下: [[email protected] ~]# yum -y install mariadbLoaded plugins: langpacks, product-id, subscription-managerThis system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.No package mariadb