hdu–1247–Hat’s Words(一般)

來源:互聯網
上載者:User
Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5772    Accepted Submission(s): 2154

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. 

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. 

OutputYour output should contain all the hat’s words, one per line, in alphabetical order. 

Sample Input

aahathathatwordhzieeword
 

Sample Output

ahathatword    
#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int MAXN=26;const int MAX=50005;char word[MAX][27];struct node{   bool is;   node *next[MAXN];   node()   {      is=false;      memset(next,0,sizeof(next));   }};void Insert(node *rt,char *s){    int i=0;    node *p=rt;    while(s[i])    {         int k=s[i++]-'a';         if(p->next[k]==NULL)            p->next[k]=new node();         p=p->next[k];    }    p->is=true; //該結點是單詞的尾
}bool Search(node *rt,char s[]){int i=0,top=0,stack[1000];node *p=rt;while(s[i]){int k=s[i++]-'a';if(p->next[k]==NULL) return 0;p=p->next[k];if(p->is && s[i])//找到該單詞含有子單詞的分割點 stack[top++]=i;//入棧}while(top)//從可能的分割點去找
{    bool ok=1;i=stack[--top];p=rt;while(s[i]){int k=s[i++]-'a';if(p->next[k]==NULL){ok=false;break;}p=p->next[k];}if(ok && p->is)//找到最後,並且是單詞的return 1;}return 0;}int main(){    int i=0;    node *rt=new node();    while(gets(word[i]))    {        Insert(rt,word[i]);        i++;}for(int j=0;j<i;j++)if(Search(rt,word[j]))   printf("%s\n",word[j]);    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.