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
思路:把每个单词分为两部分,判断两部分是否都在已经建好的字典树中
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define MAX 50010
using namespace std;
char st[MAX][105];
struct tree
{
struct tree *child[26];
int v;
}*root;
void init()
{
int i;
root=(struct tree*)malloc(sizeof(struct tree));
for(i=0; i<26; i++)
{
root->child[i]=0;
root->v=0;
}
return ;
}
void build(char *str)
{
int id,i,j,len;
len=strlen(str);
struct tree *p=root,*q;
for(i=0; i<len; i++)
{
id=str[i]-'a';
if(p->child[id]==0)
{
q=(struct tree*)malloc(sizeof(struct tree));
for(j=0; j<26; j++)
q->child[j]=0;
p->child[id]=q;
p=p->child[id];
p->v=-1;
}
else
{
p=p->child[id];
}
}
p->v=1;
return ;
}
int find(char *str)
{
int len=strlen(str);
int i,j,id;
struct tree *p=root;
for(i=0; i<len; i++)
{
id=str[i]-'a';
if(p->child[id]==0)
return -1;
p=p->child[id];
}
if(p->v==1)
return 1;
else
return -1;
}
int main()
{
int cnt,i,j,len;
char a[105],b[105];
init();
cnt=0;
while(scanf("%s",st[cnt])!=EOF)
{
build(st[cnt]);
cnt++;
}
for(i=0; i<cnt; i++)
{
len=strlen(st[i]);
for(j=0; j<len; j++)
{
memset( a,'\0',sizeof(a) );
memset( b,'\0',sizeof(b) );
strncpy(a,st[i],j);
strncpy(b,st[i]+j,len-j);
if(find(a)==1&&find(b)==1)
{
printf("%s\n",st[i]);
break;
}
}
}
return 0;
}
优质内容筛选与推荐>>
1、Redis在游戏服务器中的应用2、祛除tmx地图在滚动时可能出现的黑边3、Der Spieler und der Händler-Modus4、android获取SDcard内存大小5、ES6 Iterator 和 for...of 循环
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。
阅读
好看
已推荐到看一看
你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
取消
分享想法到看一看
确定
最多200字,当前共字
微信扫一扫
关注该公众号