• 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);
    } 
  • 相关阅读:
    redis性能优化——生产中实际遇到的问题排查总结
    Redis优化经验
    Python 发送 email 的两种方式
    封装简单的equery
    Mobiscroll的介绍【一款兼容PC和移动设备的滑动插件】
    css3毛玻璃模糊效果
    环形进度条的实现方法总结和动态时钟绘制(CSS3、SVG、Canvas)
    CSS实现圆角,三角,五角星,五边形,爱心,12角星,8角星,圆,椭圆,圆圈,八卦
    盘点8种CSS实现垂直居中水平居中的绝对定位居中技术
    CSS 去掉inline-block间隙的几种方法
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9572961.html
Copyright © 2020-2023  润新知