Hat's words

Hat's words

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
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交就会过;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值