HDU 1247 Hat’s Words(字典樹變形),hdu1247

來源:互聯網
上載者:User

HDU 1247 Hat’s Words(字典樹變形),hdu1247

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1247


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 <cstdio>#include <cstring>#include <malloc.h>#include <iostream>using namespace std;#define MAXN 26char str[50017][117];typedef struct Trie{    int v;//根據需要變化    Trie *next[MAXN];    //next是表示每層有多少種類的數,如果只是小寫字母,則26即可,    //若改為大小寫字母,則是52,若再加上數字,則是62了}Trie;Trie *root;void createTrie(char *str){    int len = strlen(str);    Trie *p = root, *q;    for(int i = 0; i < len; i++)    {        int id = str[i]-'a';        if(p->next[id] == NULL)        {            q = (Trie *)malloc(sizeof(Trie));            q->v = 1;//初始v==1            for(int j = 0; j < MAXN; j++)                q->next[j] = NULL;            p->next[id] = q;            p = p->next[id];        }        else        {        //    p->next[id]->v++;            p = p->next[id];        }    }     p->v = -1;//若為結尾,則將v改成-1表示}int findTrie(char *str){    int len = strlen(str);    Trie *p = root;    for(int i = 0; i < len; i++)    {        int id = str[i]-'a';        p = p->next[id];        if(p == NULL) //若為空白集,表示不存以此為首碼的串            return 0;     //   if(p->v == -1)   //字元集中已有串是此串的首碼     //       return -1;    }    //return -1;   //此串是字元集中某串的首碼    if(p->v == -1)//說明是完全符合        return -1;    else        return 0;}int dealTrie(Trie* T){    //動態字典樹,有時會超記憶體,這是就要記得釋放空間了    if(T==NULL)        return 0;    for(int i = 0; i < MAXN; i++)    {        if(T->next[i]!=NULL)            dealTrie(T->next[i]);    }    free(T);    return 0;}int main(){    root = (Trie *)malloc(sizeof(Trie));    for(int i = 0; i < MAXN; i++)        root->next[i] = NULL;    int cont = 0;    while(scanf("%s",str[cont])!=EOF)    {        createTrie(str[cont]);        cont++;    }    char a[117], b[117];    for(int i = 0; i < cont; i++)    {        int len = strlen(str[i]);        for(int j = 1; j < len; j++)        {            memset(a,'\0',sizeof(a));            memset(b,'\0',sizeof(b));            strncpy(a,str[i],j);            strncpy(b,str[i]+j,len-j);            if(findTrie(a)==-1 && findTrie(b)==-1)            {                printf("%s\n",str[i]);                break;            }        }    }    return 0;}



hdu1247字典樹,現等,正確了有加成想了一天了,不明白怎ACCESS_VIOLATION,有注釋牛人幫忙T^T,謝了

數組開小了
 
hdu1247,開始學字典樹,做了一下這題,為啥我的怎都輸入不了全部字串就結束了?

看一下scanf那個地方,有可能是'\n'的問題,你在scanf後面加
while(getchar() != '\n');
這樣就沒問題了應該
 

聯繫我們

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