/*字典树*/一些简单题

原理很简单,,,,,肯定能看懂,,,我觉得实现费点劲。。。。。

我的模板:

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

#define  MAX  26

typedef struct TrieNode
{
 int nCount;  // 该节点前缀 出现的次数
 struct TrieNode *next[MAX]; //该节点的后续节点
} TrieNode;

TrieNode Memory[1000000]; //先分配好内存。 malloc 较为费时
int allocp = 0;

//初始化一个节点。nCount计数为1, next都为null
TrieNode * createTrieNode()
{
 TrieNode * tmp = &Memory[allocp++];
 tmp->nCount = 1;
 for (int i = 0; i < MAX; i++)
  tmp->next[i] = NULL;
 return tmp;
}

void insertTrie(TrieNode * * pRoot, char * str)
{
 TrieNode * tmp = *pRoot;
 int i = 0, k;
 //一个一个的插入字符
 while (str[i])
 {
  k = str[i] - 'a'; //当前字符 应该插入的位置
  if (tmp->next[k])
  {
   tmp->next[k]->nCount++;
  }
  else
  {
   tmp->next[k] = createTrieNode();
  }

  tmp = tmp->next[k];
  i++; //移到下一个字符
 }

}

int searchTrie(TrieNode * root, char * str)
{
 if (root == NULL)
  return 0;
 TrieNode * tmp = root;
 int i = 0, k;
 while (str[i])
 {
  k = str[i] - 'a';
  if (tmp->next[k])
  {
   tmp = tmp->next[k];
  }
  else
   return 0;
  i++;
 }
 return tmp->nCount; //返回最后的那个字符  所在节点的 nCount
}

int main(void)
{
 char s[30];
 TrieNode *Root = createTrieNode();
 while (gets(s) && s[0] != '0') //读入0 结束
 {
  insertTrie(&Root, s);
 }

 while (gets(s)) //查询输入的字符串
 {
  printf("%d\n", searchTrie(Root, s));
 }
 return 0;
}

这个方法很快。。。。。

/*有的题要用C++不用G++要不容易超内存*/

HDU1004:

稍稍变形。。。。。。

#include<stdio.h>
#include <string.h>

using namespace std;

typedef struct node
{
    struct node *next[26];
    int cnt;
}node;

node memory[100000];
int allocp;
int ans;
char an[30];

node *create()
{
    node *tmp=&memory[allocp++];
    for(int i=0;i<26;i++) tmp->next[i]=NULL;
    tmp->cnt=0;
    return tmp;
}

int inserttrie(node **proot,char *s)
{
    node *tmp=*proot;
    int i=0,k;
    while(s[i])
    {
        k=s[i]-'a';
        if(tmp->next[k]);
        else
        {
            tmp->next[k]=create();
        }
        if(i==strlen(s)-1)
        {
            tmp->next[k]->cnt++;
            //printf("%d*\n",tmp->next[k]->cnt);
        }
        if(tmp->next[k]->cnt>=ans)
        {
            ans=tmp->next[k]->cnt;
            strcpy(an,s);
            //puts(an);
            //printf("%d\n",tmp->next[k]->cnt);
        }
        tmp=tmp->next[k];
        i++;
    }
    return ans;
}


int main()
{
    int m;
    char a[100];
    node *root=create();
    while(scanf("%d",&m)&&m!=0)
    {
        memset(memory,0,sizeof(memory));
        allocp=0;
        ans=0;
        int i;
        for(i=0;i<m;i++)
        {
            scanf("%s",a);
            inserttrie(&root,a);
        }
        printf("%s\n",an);
    }
    return 0;
}

HDU1075 可以不用字典树做,,,,用pair就好了,,模拟。。。。

字典树注意比如输入fiw,,输出还是fiw;

#include<stdio.h>
#include <string.h>

using namespace std;

char a[15],b[15];

typedef struct node
{
  char s[15];
  struct node *next[26];
};
node memory[1000000];
int allocp=0;

node * create()
{
    node * tmp=&memory[allocp++];
    for(int i=0;i<26;i++) tmp->next[i]=NULL;
    tmp->s[0]='\0';
    return tmp;
}

void inserttrie(node ** proot,char *b)
{
    node * tmp=*proot;
    int i=0,k;
    while(b[i])
    {
        k=b[i]-'a';
        if(tmp->next[k]);
        else
            tmp->next[k]=create();
        if(i==strlen(b)-1) {strcpy(tmp->next[k]->s,a);} //printf("%s****\n",tmp->next[k]->s);
        tmp=tmp->next[k];
        i++;
    }
}

int searchtrie(node *root,char *s)
{
    if(root==NULL) return 0;
    node* tmp = root;
    int i=0,k;
    while(s[i])
    {
        k=s[i]-'a';
        if(tmp->next[k])
        {
            tmp=tmp->next[k];
        }
        else return 0;
        i++;
    }
    if(tmp->s[0]==0) return 0;
    printf("%s",tmp->s);
    return 1;
}


int main()
{
    scanf("%s",a);
    char c[10001];
    char d[500];
    node * root=create();
    while(scanf("%s",a)==1)
    {
        if(strcmp(a,"END")==0) break;
        scanf("%s",b);
        int flag=0;
        inserttrie(&root,b);
    }
    scanf("%s",a);
    getchar();
    while(1)
    {
        gets(c);
        //printf("%s**\n",c);
        if(c[0]=='S'&&c[1]=='T'&&c[2]=='A'&&c[3]=='R'&&c[4]=='T') continue;
        if(c[0]=='E'&&c[1]=='N'&&c[2]=='D') break;
        int t=0;
        for(int i=0;i<strlen(c);i++)
        {
            if(c[i]>='a'&&c[i]<='z')
            {
                d[t++]=c[i];
                if(i==strlen(c)-1) {if(searchtrie(root,d)==0) printf("%s",d);}
            }
            else if((c[i-1]>='a'&&c[i-1]<='z'))
            {
                //printf("%s***\n",d);
                if(searchtrie(root,d)==0) printf("%s",d);
                printf("%c",c[i]);
                memset(d,0,sizeof(d));
                t=0;
            }
            else printf("%c",c[i]);
        }
        puts("");
    }
    return 0;
}

HDU4287

#include<stdio.h>
#include <string.h>

using namespace std;

char a[5005][10];
char b[10];

typedef struct node
{
    struct node *next[10];
    int cnt;
}node;

node memory[200000];
int allocp;

void bianhuan()
{
    for(int i=0;i<strlen(b);i++)
    {
        if(b[i]=='a'||b[i]=='b'||b[i]=='c') b[i]='2';
        if(b[i]=='d'||b[i]=='e'||b[i]=='f') b[i]='3';
        if(b[i]=='g'||b[i]=='h'||b[i]=='i') b[i]='4';
        if(b[i]=='j'||b[i]=='k'||b[i]=='l') b[i]='5';
        if(b[i]=='m'||b[i]=='n'||b[i]=='o') b[i]='6';
        if(b[i]=='p'||b[i]=='q'||b[i]=='r'||b[i]=='s') b[i]='7';
        if(b[i]=='t'||b[i]=='u'||b[i]=='v') b[i]='8';
        if(b[i]=='w'||b[i]=='x'||b[i]=='y'||b[i]=='z') b[i]='9';
    }
}

node *create()
{
    node *tmp=&memory[allocp++];
    tmp->cnt = 0;
    for(int i=0;i<10;i++)
    {
        tmp->next[i] = NULL;
    }
    return tmp;
}

void inserttrie(node **proot,char *s)
{
    node *tmp=*proot;
    int i=0,k;
    while(s[i])
    {
        k=s[i]-'0';
        if(tmp->next[k]);
        else tmp->next[k]=create();
        tmp=tmp->next[k];
        i++;
        if(i==strlen(s))
        {
            tmp->cnt++;
        }
    }
}

int searchtrie(node *root,char *s)
{
    if(root==NULL) return 0;
    node *tmp=root;
    int i=0,k;
    while(s[i])
    {
        k=s[i]-'0';
        if(tmp->next[k])
            tmp=tmp->next[k];
        else return 0;
        i++;
    }
    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<w1;i++)
            scanf("%s",a[i]);
        for(i=0;i<w2;i++)
        {
            scanf("%s",b);
            bianhuan();
            inserttrie(&root,b);
        }
        //printf("*\n");
        for(i=0;i<w1;i++)
            printf("%d\n",searchtrie(root,a[i]));
    }
    return 0;
}

字典树就差不多这样了。。。。。。。。。恩恩!



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值