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;
    }
    
  • 相关阅读:
    Unity3D 系统宏
    Mac OS—苹果搭建Android开发环境
    CocoaChina六年了,记我的这六年——六年汇总
    我的程序员之路(8)——北京第三家公司工作第一年总结
    Unity插件研究院之ResourceChecker
    Unity3D 插件大全
    MAC中开发Unity3D
    MySql安装过程(以验证)【网络资料】
    .net—— webservice的新建、发布、使用(最全、最简单)【原创】
    EF 链接MySql 建立数据模型
  • 原文地址:https://www.cnblogs.com/aeipyuan/p/9893122.html
Copyright © 2011-2022 走看看