Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11447 Accepted Submission(s): 4085
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.
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.
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
ahat
hat
hatword
hziee
word
Sample Output
ahat
hatword
hatword
Author
戴帽子的
【思路】
Trie。
对于输入字符串构造一棵Trie。枚举将字符串拆分后判断是否在Trie中存在即可。
时间复杂度为O(n*maxl*maxl)。
指针版不用考虑maxnode的问题。
另外一定注意判断输入结束为scanf ()!=1。
【代码】
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int maxl = ;
const int sigmasize = ;
/*
//静态分配内存版本
const int maxnode = 400000;
struct Trie{
int ch[maxnode][sigmasize];
int val[maxnode];
int sz; Trie() {
sz=1;
memset(ch[0],0,sizeof(ch[0]));
val[0]=0;
}
int idx(char c) { return c-'a'; }
void insert(char* s,int v) {
int n=strlen(s),u=0;
for(int i=0;i<n;i++) {
int c=idx(s[i]);
if(!ch[u][c]) {
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=v;
}
int find(char* s) {
int n=strlen(s),u=0;
for(int i=0;i<n;i++) {
int c=idx(s[i]);
if(!ch[u][c]) return 0;
else u=ch[u][c];
}
return val[u];
}
}trie;
*/
//动态分配内存版本
struct Node{
int val;
Node* next[sigmasize];
};
struct Trie{
Node *root;
Trie() {
root=new Node;
for(int i=;i<sigmasize;i++) root->next[i]=NULL;
root->val=;
}
int idx(char c) { return c-'a'; }
void insert(char* s,int v) {
int n=strlen(s);
Node* u=root;
for(int i=;i<n;i++) {
int c=idx(s[i]);
if(u->next[c]==NULL) {
Node* tmp=new Node;
tmp->val=;
for(int i=;i<sigmasize;i++) tmp->next[i]=NULL;
u->next[c]=tmp;
}
u=u->next[c];
}
u->val=v;
}
int find(char* s) {
int n=strlen(s);
Node* u=root;
for(int i=;i<n;i++) {
int c=idx(s[i]);
if(u->next[c]==NULL) return ;
else u=u->next[c];
}
return u->val;
}
void del(Node *root) {
for(int i=;i<sigmasize;i++) {
if(root->next[i]!=NULL) del(root->next[i]);
}
free(root);
}
}trie; int n=;
char s[][maxl]; int main() {
//freopen("cin.in","r",stdin);
//freopen("cout.out","w",stdout);
while(scanf("%s",s[n])==) // 注意是scanf==1判断
{
trie.insert(s[n],);
n++;
}
char t1[maxl],t2[maxl];
FOR(i,,n-)
{
int len=strlen(s[i]);
FOR(j,,len-) {
strcpy(t1,s[i]);
t1[j]='\0';
strcpy(t2,s[i]+j);
if(trie.find(t1) && trie.find(t2)) {
printf("%s\n",s[i]);
break;
}
}
}
trie.del(trie.root); //内存回收
return ;
}