码迷,mamicode.com
首页 > 其他好文 > 详细

Hat’s Words

时间:2016-10-28 03:23:16      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:panel   limit   script   false   运行时间   com   长度   ber   its   

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;
}

 

Hat’s Words

标签:panel   limit   script   false   运行时间   com   长度   ber   its   

原文地址:http://www.cnblogs.com/wuwangchuxin0924/p/6006236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!