hdu 1251 統計難題

2021-07-03 05:34:58 字數 1187 閱讀 2523

ignatius最近遇到乙個難題,老師交給他很多單詞(只有小寫字母組成,不會有重複的單詞出現),現在老師要他統計出以某個字串為字首的單詞數量(單詞本身也是自己的字首).

輸入資料的第一部分是一張單詞表,每行乙個單詞,單詞的長度不超過10,它們代表的是老師交給ignatius統計的單詞,乙個空行代表單詞表的結束.第二部分是一連串的提問,每行乙個提問,每個提問都是乙個字串.

注意:本題只有一組測試資料,處理到檔案結束.

對於每個提問,給出以該字串為字首的單詞的數量.

banana

band

beeabsolute

acmba

bband

abc

231

這個題是一道字典樹的題目, 算是入門練手的把 ,下面將用兩種方法來寫

乙個是指標建樹 的寫法,,另乙個是用陣列來模擬

法一:

用字典樹模版

#include#include#includeusing namespace std;

const int kind=26;

struct node

p=p->zhi[k];

p->count++; }}

/*void clear(node *p)

delete p;

root=null;

return ;

}*/int find(char *s)

if(s[i]) return 0;

return p->count;}

int main()

while(gets(s))

//clear(root);

return 0;

}

法二: 陣列模擬

#include#include#define maxn 400000

int shu[maxn][26];

int pass[maxn];

int main()

{ int i;

char s[20];

int q=1,cur;

memset(shu,-1,sizeof(shu));

while(gets(s),strlen(s)>0)

{ int l=strlen(s);

cur=0;

for(i=0;i

統計難題 hdu1251

昨天在小策策 和 小超人的指導下學習了下字典樹,於是今天寫了下這個很早就想學的演算法 寫的很醜,而且還看了下課件的 include 2497868 2010 05 28 21 25 42 accepted 1251 93ms 43784k 1152 b c 悔惜晟 include include u...

HDU 1251 統計難題

problem description ignatius最近遇到乙個難題,老師交給他很多單詞 只有小寫字母組成,不會有重複的單詞出現 現在老師要他統計出以某個字串為字首的單詞數量 單詞本身也是自己的字首 input 輸入資料的第一部分是一張單詞表,每行乙個單詞,單詞的長度不超過10,它們代表的是老師...

hdu 1251統計難題

字典樹模版題 動態實現 include include include define maxn 26 struct node root void insert char str else int find char str return current count int main 靜態實現 inc...