hdu1247(Hat’s Words)

來源:互聯網
上載者:User

標籤:ext   get   string   os   set   name   

我以為像a、aa這樣的輸入應該是沒有輸出的,結果還是要輸出aa。

建樹的時候就是常規建樹,不過尋找的時候要做一些變形:對於一個單詞,從第一位檢查有沒有單詞是它的首碼,如果有的話,再去檢查它的後半部分是不是一個獨立的單詞,要滿足這兩次尋找才能輸出。

題意:給一些單詞(以字典序輸入),找出那些可以分成另外的兩個單詞的單詞,以字典序輸出;

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
typedef struct Node
{
    int flag;
    struct Node *next[26];
}Node,*Tree;
char a[50001][30];
void Creat(Tree &T)
{
    T=(Node *)malloc(sizeof(Node));
    T->flag=0;
    for(int i=0;i<26;i++)
    {
        T->next[i]=NULL;
    }
}
void insert(Tree &T,char *s)
{
    Tree p=T;
    int t;
    int l=strlen(s);
    for(int i=0;i<l;i++)
    {
        t=s[i]-‘a‘;
        if(p->next[t]==NULL)
        {
            Creat(p->next[t]);
        }
        p=p->next[t];
    }
    p->flag=1;
}
int search(Tree T,char *s)
{
    int t;
    Tree p=T;
    int l=strlen(s);
    for(int i=0;i<l;i++)
    {
        t=s[i]-‘a‘;
        if(p->next[t]==NULL)
            return 0;
        p=p->next[t];
    }
    if(p->flag) return 1;
    else return 0;
}
void D(Tree p)
{
    for(int i=0;i<26;i++)
    {
        if(p->next[i]!=NULL)
            D(p->next[i]);
    }
    free(p);
}
int main()
{
     char s1[30];
     char s2[30];
     Tree T;
     int kk=0;
     Creat(T);
     while(gets(a[kk])&&strlen(a[kk]))
     {
         insert(T,a[kk]);
        kk++;
     }
     int i,j;
     for(i=0;i<kk;i++)
     {
         int l=strlen(a[i]);
         for(j=0;j<l;j++)
         {
             memset(s1,‘\0‘,sizeof(s1));
             memset(s2,‘\0‘,sizeof(s2));
             int kkk=0;
             int jjj=0;
             for(int kk=0;kk<=j;kk++)
                s1[kkk++]=a[i][kk];
              for(int jj=j+1;jj<l;jj++)
              {
                  s2[jjj++]=a[i][jj];
              }
              int qq=search(T,s1);
              int ww=search(T,s2);
              if(qq&&ww)
              {
                  printf("%s\n",a[i]);
                  break;
              }
             
         }
     }
     D(T);
    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.