题意:给你n个单词,这些单词组成一个单词表。然后,在对这其中的的每个单词进行拆分(拆成两个单词),判断是否拆分完的单词是否都在这个单词表中,如果是输入该单词。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct dirtree
{
struct dirtree *child[26];
bool vis;
};
char word[50000][100];
struct dirtree *root;
void BuildTree(char *word)
{
int len=strlen(word);
struct dirtree *Root,*tem;
Root=root;
for(int i=0; i<len; i++)
{
if(Root->child[word[i]-'a']==0)
{
tem=(struct dirtree *)malloc(sizeof(struct dirtree));
for(int ii=0; ii<26; ii++)
{
tem->child[ii]=0;
tem->vis=false;
}
Root->child[word[i]-'a']=tem;
}
Root=Root->child[word[i]-'a'];
}
// printf("zs\n");
Root->vis=true;
}
bool find(char *word)
{
int len=strlen(word);
struct dirtree *Root;
Root=root;
for(int i=0; i<len; i++)
{
if(Root->child[word[i]-'a']!=0)
Root=Root->child[word[i]-'a'];
else
return false;
}
if(Root->vis)
return true;
else
return false;
}
int main()
{
int len=0;
char tem1[100],tem2[100];
root=(struct dirtree *)malloc(sizeof(struct dirtree));
for(int i=0; i<26; i++)
{
root->child[i]=0;
root->vis=false;
}
while(scanf("%s",word[len])!=EOF)
{
BuildTree(word[len]);
len++;
}
for(int ii=0; ii<len; ii++)//对单词进行拆分
{
int Len=strlen(word[ii]);
for(int i=0; i<Len-1; i++)
{
int aL=0,bL=0;
for(int jj=0; jj<=i; jj++)
{
tem1[aL]=word[ii][jj];
aL++;
}
for(int j=i+1; j<Len; j++)
{
tem2[bL]=word[ii][j];
bL++;
}
tem1[aL]=tem2[bL]='\0';
if(find(tem1)&&find(tem2))
{
printf("%s\n",word[ii]);
break;
}
memset(tem1,0,sizeof(tem1));
memset(tem2,0,sizeof(tem2));
}
}
return 0;
}
/*
a
ahata
ahat
hat
hatword
hziee
word
za
zaa
p
aahat
p
a
za
zaa
hata
ahata
aahat
p
*/