zoukankan      html  css  js  c++  java
  • HDU1247-Hat’s Words

    题目链接

    Hat’s Words

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


     

    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

    题目大意:给出的字符串任意两个组合,是否能组合出原来已经存在的字符串,可以的话,组合出来的就是讨厌的。

    思路:字典树,然后暴力把每个单词分成两部分,都能找到,就是讨厌的

    AC代码:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    char s[50010][100];
    typedef struct Trie_Node {
    	struct Trie_Node* next[26];
    	bool isword;
    	int num; 
    	Trie_Node() {//初始化 
    		for(int i = 0; i < 26; i++) {
    			next[i] = NULL;
    		}
    		isword = false;//单词结束标志 
    		num = 0;//到达该处的字符串数量 
    	}
    }trie; 
    
    void insert(trie* root, char* s) {
    	trie* p = root;
    	int i = 0;
    	while(s[i] != '') {
    		if(p->next[s[i]-'a'] == NULL) {//为空,就开,然后指向它 
    			trie* temp = new trie();
    			p->next[s[i]-'a'] = temp;
    		}
    		p = p->next[s[i]-'a'];//继续跑 
    		i++;
    		p->num++;//到达该处的字符串++ 
    	}
    	p->isword = true;//单词结束 
    }
    
    bool search(trie* root, char* s) {
    	trie* p = root;
    	for(int i = 0; s[i] != ''; i++) { 
    		if(p->next[s[i]-'a'] == NULL)//找不到该单词, 
    			return false;
    		p = p->next[s[i]-'a'];
    	}
    	return p->isword; 
    }
    
    void del(trie *root) {
    	for(int i = 0; i < 26; i++) {
    		if(root->next[i] != NULL)
    			del(root->next[i]);
    	}
    	free(root);
    } 
    
    int main() {
    	int cnt = 0;
    	trie* root = new trie();
    	while(~scanf("%s", s[cnt])) {//输入巧妙 
    		insert(root, s[cnt++]);
    	}
    	for(int i = 0; i < cnt; i++) {
    		int len = strlen(s[i]);
    		for(int j = 1; j < len-1; j++) {//暴力分解 
    			char temp1[50] = {''};
    			char temp2[50] = {''};
    			strncpy(temp1, s[i], j);//从0 到j 
    			strncpy(temp2, s[i]+j, len-j);//从j+1 到 结束 
    			if(search(root, temp1) && search(root, temp2)) {
    				printf("%s
    ", s[i]);
    				break;
    			}
    		}
    	}
    	del(root);
    } 
  • 相关阅读:
    Java并发基础10:原子性操作类的使用
    Java并发基础09. 多个线程间共享数据问题
    Java并发基础08. 造成HashMap非线程安全的原因
    Java并发基础07. ThreadLocal类以及应用技巧
    Java并发基础06. 线程范围内共享数据
    Java并发基础05. 传统线程同步通信技术
    Java并发基础04. 线程技术之死锁问题
    我是如何从通信转到Java软件开发工程师的?
    IOS 判断耳机插入/拔出
    海量数据处理
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9572961.html
Copyright © 2011-2022 走看看