题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1247
最简单的字典树,初始化错了,找了好久都不知道为什么WA
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define maxn 50065
#define N 50
struct node
{
node *nxt[N];
int isend;
node()
{
isend = 0;
memset(nxt,0,sizeof(nxt));
}
};
node* rt;
char ls[maxn][50];
void insert(char c[])
{
node* t = rt;
for(int i = 0 ; c[i] ; ++i)
{
int x = c[i] - 'a';
if( t->nxt[x] == NULL)
t->nxt[x] = new node();
t = t->nxt[x];
}
t->isend = 1;
}
int check(char c[])
{
node* t = rt;
for(int i = 0 ; c[i] ; ++i)
{
int x = c[i] - 'a';
if( t->nxt[x] == NULL )return 0;
t = t->nxt[x];
}
return t->isend;
}
int main()
{
rt = new node();
int k = 0 ;
while(scanf("%s",ls[k])!=EOF)
{
insert(ls[k]);
k++;
}
for(int i = 0 ; i < k ; ++i)
{
for(int j = 1 ; j < strlen(ls[i]) ; ++j)
{
char a[50] = {'\0'};
char b[50] = {'\0'};
strncpy(a,ls[i],j);
strncpy(b,ls[i]+j,strlen(ls[i])-j);
if( check(a) && check(b) )
{
printf("%s\n",ls[i]);
break;
}
}
}
}