Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16230 Accepted Submission(s): 5790
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
戴帽子的
Recommend
Ignatius.L | We have carefully selected several similar problems for you:
题意是 在输入的字符串中,如果一个字符串能由另外两个字符串拼接而成,就输出这个字符串
分析: 枚举每一个字符串分成两个字符串的结果,看这两个字符串是否能够找到
代码如下:
#include#include #include using namespace std;const int MAXN=26; //只有小写字母typedef struct Trie{ bool v; Trie *next[MAXN];}Trie; Trie *root;char s1[100];char s2[100];void createTrie(char *str){ int len=strlen(str); Trie *p=root,*q; for(int i=0;i next[id]==NULL) { q=(Trie*)malloc(sizeof(Trie)); q->v=false; //每次建立新节点进行初始化操作 for(int j=0;j next[j]=NULL; p->next[id]=q; p=p->next[id]; } else { // p->next[id]->v=false; p=p->next[id]; } } if(p!=root) //p==root 代表读入的是空串 p->v=true; //表示以此结点结尾的字符串存在}bool findTrie(char *str){ int len=strlen(str); Trie *p=root; for(int i=0;i next[id]; if(p==NULL) return false; } return p->v; }int deal(Trie *T) { int i; if(T==NULL) return 0; for(int i=0;i next[i]!=NULL) deal(T->next[i]); } free(T); return 0;}int main(){ char str[50010][100]; int t,n,flag,cnt; root=(Trie*)malloc(sizeof(Trie)); for(int i=0;i next[i]=NULL; root->v=false; //初始化 cnt=0; while(gets(str[cnt])) { createTrie(str[cnt]); cnt++; } // cout< <<" "< <