统计难题 hdu1251

/* 
    昨天在小策策 和 小超人的指导下学习了下字典树,于是今天写了下这个很早就想学的算法
    代码写的很丑,而且还看了下课件的
*/

#include<iostream>//2497868 2010-05-28 21:25:42 Accepted 1251 93MS 43784K 1152 B C++ 悔惜晟
#include<cstring>
#include<cstdio>
using namespace std;

struct trie
{
 struct trie *child[26];
 int n;
};

struct trie *root;

void insert(char *str)
{
 int i, j, len;
 len = strlen(str);
 if(len == 0)
  return ;
 struct trie *P, *N;
 P = root;
 for(i = 0 ; i < len; i++)
 {
  if(P->child[str[i] - 'a'] != NULL)
  {
   P = P->child[str[i] - 'a'];
   P->n = P->n + 1;
  }
  else
  {
   N = (struct trie *)malloc(sizeof(struct trie ));
   for(j = 0; j < 26; j++)
    N->child[j] = NULL;
   P->child[str[i] - 'a'] = N;
   P = N;
   P->n = 1;
  }
 }
}

int find(char *str)
{
 int i, len, m;
 len = strlen(str);
 struct trie *P;
 P = root;
 for(i = 0; i < len; i++)
 {
  if(P->child[str[i] - 'a'] != 0)
  {
   P = P->child[str[i] - 'a'];
   m = P->n;
  }
  else
   return 0;
 }
 return m;
}

int main()
{
 char str[20];
 root = (struct trie *)malloc(sizeof(struct trie));
 int i;
 for(i = 0; i < 26; i++)
  root->child[i] = NULL;
 while(gets(str))
 {
  if(strcmp(str, "") == 0)
   break;
  insert(str);
 }
 while(gets(str) )
 {
  printf("%d/n", find(str));
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值