hdu1247Hat’s Words

來源:互聯網
上載者:User

字典樹,感覺有點暴力。

Hat’s Words Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 4   Accepted Submission(s) : 4Font: {
ProFont('Times New Roman')
}">Times New Roman | {
ProFont('Verdana')
}">Verdana | {
ProFont('Georgia')
}">Georgia Font Size: {
ProFontAdd(-1)
}">← {
ProFontAdd(1)
}">→{
ObjFolder('procon')
}">Problem DescriptionA 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.{
ObjFolder('proinput')
}">InputStandard 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.{
ObjFolder('prooutput')
}">OutputYour output should contain all the hat’s words, one per line, in alphabetical order. {
ObjFolder('prosamplein')
}">Sample Input
aahathathatwordhzieeword
{
ObjFolder('prosampleout')
}">Sample Output
ahathatword<code>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
char str[50005][100],str1[100],str2[100];
struct dirtree
{
   struct dirtree *child[26];
   bool hash;
};
struct dirtree *root;
void Insert(char*source)
{
     int i,j,len;
     len=strlen(source);
     if(len==0)return ;
     struct dirtree *current,*newnode;
     current=root;
     for(i=0;i<len;i++)
     {
        if(current->child[source[i]-'a']!=0)
        {
           current=current->child[source[i]-'a'];
           if(i==len-1)
           {
              current->hash=true;
              break;
           }
          
        }
        else
        {
           newnode=(struct dirtree*)malloc(sizeof(struct dirtree));
           for(j=0;j<26;j++)
           {
              newnode->child[j]=0;
              newnode->hash=false;
           }
           current->child[source[i]-'a']=newnode;
           current=newnode;
           if(i==len-1)
           {
              current->hash=true;
              break;
           }
           
        }
     }
}
bool Find(char *source)
{
    struct dirtree *current;
    int len=strlen(source);
    if(len==0)return 0;
    current=root;
    for(int i=0;i<len;i++)
    {
       if(current->child[source[i]-'a']!=0)
          current=current->child[source[i]-'a'];
       else
          return false;
    }
    return current->hash;
}
int main()
{
 
   int len = 0;
   int i, j, k, len1;
   root = (struct dirtree *)malloc(sizeof(struct dirtree));
   for(i = 0; i < 26; i++)
   {
      root->child[i] = 0;
      root->hash = false;
   }
   while(scanf("%s",str[len])!=EOF)
   {
       Insert(str[len]);
       len++;
   }
   for(i=0;i<len;i++)
   {
     len1=strlen(str[i]);
     for(j=1;j<len1;j++)
     {
        for(k=0;k<j;k++)
        {
           str1[k]=str[i][k];
        }
        str1[k]='/0';
        int t=0;
        for(k=j;k<len1;k++)
        {
           str2[t++]=str[i][k];
        }
        str2[t]='/0';
        if(Find(str1)&&Find(str2))
        {
           printf("%s/n",str[i]);
           break;
        }
     }
   }
   //system("pause");
   return 0;
  
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.