【POJ】2503 Babelfish

來源:互聯網
上載者:User

標籤:des   style   class   blog   code   ext   

字典樹簡單題。

 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4  5 typedef struct Trie { 6     Trie *next[26]; 7     char str[15]; 8 } Trie; 9 10 Trie root;11 12 void create(char str[], char des[]) {13     int i = 0, j, id;14     Trie *p = &root, *q;15 16     while (str[i]) {17         id = str[i] - ‘a‘;18         if (p->next[id] == NULL) {19             q = (Trie *)malloc(sizeof(Trie));20             for (j=0; j<26; ++j)21                 q->next[j] = NULL;22             p->next[id] = q;23         }24         p = p->next[id];25         ++i;26     }27     strcpy(p->str, des);28 }29 30 void find(char str[]) {31     int i = 0, id;32     Trie *p = &root;33 34     while (str[i]) {35         id = str[i] - ‘a‘;36         if (p->next[id] == NULL) {37             printf("eh\n");38             return ;39         }40         p = p->next[id];41         ++i;42     }43     printf("%s\n", p->str);44 }45 46 void del(Trie *t) {47     int i;48     if (t == NULL)49         return ;50     for (i=0; i<26; ++i)51         del(t->next[i]);52     free(t);53 }54 55 int main() {56     char src[12], des[25];57     char *p;58 59     for (int i=0; i<26; ++i)60         root.next[i] = NULL;61 62     while (gets(des)!=NULL && des[0]!=‘\0‘) {63         p = strchr(des, ‘ ‘);64         strcpy(src, p+1);65         *p = ‘\0‘;66         create(src, des);67     }68 69     while (scanf("%s", src) != EOF)70         find(src);71 72     return 0;73 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.