字典樹 一些簡單題

2021-07-04 03:53:16 字數 2331 閱讀 2146

原理很簡單,,,,,肯定能看懂,,,我覺得實現費點勁。。。。。

我的模板:

#include

#include

using namespace std;

#define  max  26

typedef struct trienode

trienode;

trienode memory[1000000]; //先分配好記憶體。 malloc 較為費時

int allocp = 0;

//初始化乙個節點。ncount計數為1, next都為null

trienode * createtrienode()

void inserttrie(trienode * * proot, char * str)

else

tmp = tmp->next[k];

i++; //移到下乙個字元

}int searchtrie(trienode * root, char * str)

else

return 0;

i++;

}return tmp->ncount; //返回最後的那個字元  所在節點的 ncount

}int main(void)

while (gets(s)) //查詢輸入的字串

return 0;

}這個方法很快。。。。。

/*有的題要用c++不用g++要不容易超記憶體*/

hdu1004:

稍稍變形。。。。。。

#include

#include

using namespace std;

typedef struct node

node;

node memory[100000];

int allocp;

int ans;

char an[30];

node *create()

int inserttrie(node **proot,char *s)

if(i==strlen(s)-1)

if(tmp->next[k]->cnt>=ans)

tmp=tmp->next[k];

i++;

}return ans;

}int main()

;node memory[1000000];

int allocp=0;

node * create()

void inserttrie(node ** proot,char *b)

//printf("%s****\n",tmp->next[k]->s);

tmp=tmp->next[k];

i++;}}

int searchtrie(node *root,char *s)

else return 0;

i++;

}if(tmp->s[0]==0) return 0;

printf("%s",tmp->s);

return 1;

}int main()

scanf("%s",a);

getchar();

while(1)

}else if((c[i-1]>='a'&&c[i-1]<='z'))

else printf("%c",c[i]);

}puts("");

}return 0;

}hdu4287

#include

#include

using namespace std;

char a[5005][10];

char b[10];

typedef struct node

node;

node memory[200000];

int allocp;

void bianhuan()

return tmp;

}void inserttrie(node **proot,char *s)}}

int searchtrie(node *root,char *s)

return tmp->cnt;

}int main()

{int m;

scanf("%d",&m);

int w1,w2;

while(m--)

{memset(memory,0,sizeof(memory));

allocp=0;

node *root=create();

scanf("%d %d",&w1,&w2);

int i;

for(i=0;i字典樹就差不多這樣了。。。。。。。。。恩恩!



字典樹的一些使用

字典樹的一些簡單使用 字典樹又被叫做tire樹,又被叫做字首樹,顧名思義就是可以用來求字串字首的樹形結構,與二叉樹不同,字典樹是多叉樹,字串不是直接存放在節點中,而是每乙個節點存放乙個字元,乙個節點的孩子是具有相同的字串字首的。字典樹的根節點通常是取空的。字典樹的特點是 1.所有相同的字首只儲存一次...

一些簡單的樹狀陣列題

大意 給定一列數 a i 求滿足下列條件的數對 x,y 的數量 1 xn 與 a i n 是等價的,所以直接將大於n的 a i 賦為 n 可以避免離散化 include include include include include include include include using nam...

一些簡單的樹狀陣列題

大意 給定一列數 a i 求滿足下列條件的數對 x,y 的數量 1 xn 與 a i n 是等價的,所以直接將大於n的 a i 賦為 n 可以避免離散化 include include include include include include include include using nam...