zoukankan      html  css  js  c++  java
  • HDU 1247 Hat’s Words(字典树活用)

    Hat’s Words
    Time Limit : 2000 / 1000 MS(Java / Others)    Memory Limit : 65536 / 32768 K(Java / Others)
    Total Submission(s) : 18969    Accepted Submission(s) : 6689


    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

    题意:按字典序输入几个单词,按字典序输出由另外两个单词组成的单词

    分析:先建树,后查询每个单词,如果查询到第一个原有单词再接着查询剩余部分,剩余部分也是原有单词组成则输出该单词

    #include<iostream>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    char temp[50001][20];
    bool vis[300001];
    int t[300001][30],pos=1,num[300001];
    void insert(char *s)//建树
    {
    	int rt = 0;
    	int len = strlen(s);
    	for (int i = 0; i < len; i++)
    	{
    		int x = s[i] - 'a';
    		if (!t[rt][x])
    			t[rt][x] = pos++;
    		rt = t[rt][x];
    	}
    	vis[rt] = 1;//标记
    }
    bool search1(char *s)//查询后部分是否是已有单词构成
    {
    	int rt = 0;
    	for (int i = 0; s[i]; i++)
    	{
    		int x = s[i] - 'a';
    		if (!t[rt][x])
    			return 0;
    		rt = t[rt][x];
    	}
    	if (vis[rt])//验证尾结点是否为查到的单词的尾结点
    		return 1;
    	else
    		return 0;
    }
    bool search(char *s)//前部分
    {
    	int rt = 0;
    	for (int i = 0; s[i]; i++)
    	{
    		int x = s[i] - 'a';
    		if (vis[rt] && search1(s + i))//rt为前部分单词的尾结点,验证后部分是否为已有单词
    			return 1;
    		rt = t[rt][x];
    	}
    	return 0;
    }
    int main()
    {
    	int i = 0;
    	while (~scanf("%s", temp[i]))
    		insert(temp[i++]);
    	for (int j = 0; j <i; j++)
    	{
    		//cout << temp[j] << endl;
    		if (search(temp[j]))
    			printf("%s
    ", temp[j]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    检查 Linux 服务器性能
    openssl、x509、crt、cer、key、csr、ssl、tls 这些都是什么鬼?
    Jenkins+ Xcode+ 蒲公英 实现IOS自动化打包和分发
    如何在 Linux 上录制你的终端操作
    Swift 开源 Linux Ubuntu Install
    SCP 命令
    ubuntu openfire Server install
    git 和 svn 的比较
    implicitly declaring function 'malloc' with type void *(unsigned long ) 错误 解决
    python 使用缓存加快运算
  • 原文地址:https://www.cnblogs.com/aeipyuan/p/9893122.html
Copyright © 2011-2022 走看看