Hat's words
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 2
Total Submission(s) : 6 Accepted Submission(s) : 2
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
题意:将字符串分成两个部分,查找两个子字符串是否在给出的字符串中出现,出现则输出原字符串;
这题运用了字典树,看了网上的解题报告做出来的,这题注意的是将一个字符串分成两个字符串的方法,很有用,还有一个是查找一个字符串是否出现时判断长度是否和该点的长度相等,故在结构体中用len记住字符串的长度;
<span style="background-color: rgb(255, 255, 255);"><span style="font-family:SimSun;font-size:14px;">#include <stdlib.h>
#include <string.h>
char str[50050][25];
typedef struct tree
{
int len;
struct node *next[26];
} node ,*Node;
Node root=NULL;
void insert(char *str)
{
Node now=NULL,next=NULL;
int len=strlen(str);
int index,i;
now=root;
for(i=0; i<len; i++)
{
index=str[i]-'a';
if(now->next[index]!=NULL)
now=now->next[index];
else
{
next=(Node)calloc(1,sizeof(node));
now->next[index]=next;
now=next;
}
}
now->len=len;
}
int search(char *str)
{
Node now=root;
int len=strlen(str);
int i;
for(i=0; i<len; i++)
{
int index=str[i]-'a';
if(now->next[index]==NULL)
return 0;
now=now->next[index];
}
if(now->len==len)
return 1;
return 0;
}
int main()
{
int i=0,j,m,n;
char a[50050],b[50050];
root=(Node)calloc(1,sizeof(node));
while(scanf("%s",str[i])!=EOF)
{
getchar();
insert(str[i]);
i++;
}
int x=i;
for(i=0; i<x; i++)
{
m=0,n=0;
int len=strlen(str[i]);
for(j=1; j<len; j++)
{
a[m]=str[i][m];
a[++m]='\0';
for(n=j; n<len; n++)
{
b[n-j]=str[i][n];
}
b[n-j]='\0';
if(search(a)&&search(b))
{
printf("%s\n",str[i]);
break;
}
}
}
return 0;
}</span></span>还有一个搞不懂的为什么用GNU C++交 会CE,用C交就会过;
449

被折叠的 条评论
为什么被折叠?



