本文主要是介绍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): 19842 Accepted Submission(s): 6968
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
Author
戴帽子的
#include<iostream>
#include<cstring>
using namespace std;
struct node{struct node *p[26];int flag;node(){for( int i=0;i<26;i++){ p[i] = NULL; }flag = 0; }
};
struct node * root =NULL;
char buf[50005][59];
void insert( char str[] ){struct node* rot = root;int len = strlen( str); for( int i=0;i<len;i++){int id = str[i] - 'a';if( rot->p[id] == NULL ){rot->p[id] = new struct node;} rot = rot->p[id];}rot->flag = 1;
}
int query( char str[] ){struct node* rot = root;int id;int len = strlen( str );for( int i=0;i<len;i++){id = str[i] - 'a';if( rot->p[id] == NULL )return 0;else rot = rot->p[id];} return rot->flag;
}
int main(void){int cnt =0;root = new struct node;while( scanf("%s",buf[cnt] ) !=EOF ){insert( buf[cnt++] ); }for( int i=0;i<cnt;i++){int len = strlen( buf[i]);for(int j=0;j<len-1;j++ ){char str1[109],str2[109];memset( str1,0,sizeof(str1));memset( str2,0,sizeof(str2));for( int k=0;k<=j;k++ )str1[k] = buf[i][k];str1[j+1] = 0;int index = 0;for( int k=j+1;k<len;k++){str2[index++] = buf[i][k];}str2[index] = 0 ;if( query( str1 ) && query( str2 ) ){printf("%s\n",buf[i]);break;} }} return 0;
}
这篇关于HDU 1247 Hat’s Words的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!