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;
    }
    
  • 相关阅读:
    TensorFlow conv2d实现卷积
    tensorflow max_pool(最大池化)应用
    tensorflow Relu激活函数
    tensorflow Sigmoid 应用
    tensorflow softmax应用
    并发和并行的区别
    eclipse常用快捷键即项目操作
    zookeeper学习一
    TCP的三次握手与四次挥手
    python编码问题unicode&str
  • 原文地址:https://www.cnblogs.com/aeipyuan/p/9893122.html
Copyright © 2011-2022 走看看