日常算法

#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大写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值