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);
    } 
  • 相关阅读:
    013开发板系统安装准备
    012开发板串口连接
    011OK6410开发板介绍
    010GCC程序编译
    009Linux密码故障排除
    vue 中的solt的用法
    vue solt的应用场景
    Typescript 用接口模拟ajax请求
    Typescript方法重载实现系列二
    Typescript中方法重载的实现
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9572961.html
Copyright © 2011-2022 走看看