#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node{
//char alpha;
int count;
struct Node *child [26];
}TNode,*Tptr;
Tptr root;
int insert(char * word)
{
Tptr p = root;
int i,tmp;
for(i=0;word[i]!='\0';i++)
{
//printf("%c",word[i]);
tmp = word[i]-'a';
if(p->child[tmp]!=NULL)
{
p->child[tmp]->count++;
}
else
{//printf("%s","S");
Tptr temp;
temp = (Tptr)malloc(sizeof(TNode));
memset(temp->child, NULL, sizeof(temp->child));
p->child[tmp] = temp;
temp->count = 1;
}
p = p->child[tmp];
}
}
int search(char * pre)
{
Tptr p = root;
int i,tmp;
for(i=0;pre[i]!='\0';i++)
{
tmp = pre[i]-'a';
if(p->child[tmp]==NULL)
{
return 0;
}
p = p->child[tmp];
}
return p->count;
}
int main(void)
{
int word_count,i,pre_count,count;
root = (Tptr)malloc(sizeof(TNode));
memset(root->child, NULL, sizeof(root->child));
scanf("%d",&word_count);
//char * word_list[]
char word[20];
char pre[20];
for(i=0;i<word_count;i++)
{
scanf("%s",word);
// printf("%s\n",word);
insert(word);
}
scanf("%d",&pre_count);
int tmp[word_count];
for(i=0;i<pre_count;i++)
{
scanf("%s",pre);
count = search(pre);
tmp[i] = count;
// printf("%d\n",i);
}
for(i=0;i<pre_count;i++)
{
//scanf("%d\n",);
printf("%d\n",tmp[i]);
}
}
Trie树问题
https://hihocoder.com/problemset/problem/1014
定义数据结构
typedef struct Node{
//char alpha;
int count;
struct Node *child [26];
}TNode,*Tptr;
初始化
Tptr temp;
temp = (Tptr)malloc(sizeof(TNode));
memset(temp->child, NULL, sizeof(temp->child));
temp->count = 1;
问题
1.全局变量的使用
2.遍历字符数组
char * p
(1
for(;*p=‘\0’;p++)
(2
for(i=0;p[i]!='\0';i++)
3.char *p;
scanf("%s",p);未分配内存直接读入错误
4.指针大小->sizeof (void*) 32位->4 byte 64位->8 byte
5.struct 内存占用问题
http://blog.csdn.net/hudashi/article/details/7400684
空的struct 1 byete
6.二维字符串数组
char * word_list[22];
word_list[1]="sss";
7.指针->声明空间->初始化值(memset(root->child, NULL, sizeof(root->child));)
不这样操作就使用值会错误
8.pointer->成员, stuct.成员
TNode test;
Tptr root;
test.count=0;
root->count=0;
9.''->字符 ""->字符串 NULL大写