杭电 hdu 1305 Immediate Decodability


/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2011 panyanyany All rights reserved. URL : http://acm.hdu.edu.cn/showproblem.php?pid=1305 Name : 1305 Immediate Decodability Date : Wednesday, August 24, 2011 Time Stage : half an hour Result: 4486760 2011-08-24 21:16:51 Accepted 1305 0MS 240K 1929 B C++ pyy Test Data: Review: //----------------------------------------------------------------------------*/ #include <stdio.h> #include <string.h> #include <stdlib.h> #define max(a, b) (((a) > (b)) ? (a) : (b)) #define min(a, b) (((a) < (b)) ? (a) : (b)) #define infinity 0x0f0f0f0f #define minus_inf 0x80808080 #define MAXSIZE 1009 typedef struct tagNODE { int cnt ; struct tagNODE * child[2] ; } NODE ; char str[22] ; int can ; int stkPtr ; NODE * stack[MAXSIZE] ; NODE * tmp, * newNode ; #define root stack[0] void insert (char *patn) { int i, j ; int len = strlen (patn) ; tmp = root ; for (i = 0 ; i < len - 1 ; ++i) { j = patn[i] - '0' ; if (!tmp->child[j]) { newNode = (NODE *) calloc (1, sizeof (NODE)) ; stack[stkPtr++] = newNode ; tmp->child[j] = newNode ; } tmp = tmp->child[j] ; if (tmp->cnt) can = 0 ; } // 我用的判断方法貌似不太美观,看了下别人的代码, // 通过判断下一层的孩子指针是否存在,可以达到同样的效果 j = patn[i] - '0' ; if (tmp->child[j]) { can = 0 ; } else { newNode = (NODE *) calloc (1, sizeof (NODE)) ; stack[stkPtr++] = newNode ; tmp->child[j] = newNode ; } tmp = tmp->child[j] ; ++tmp->cnt ; } void recycle () { while (stkPtr) free (stack[--stkPtr]) ; } int main () { int i, j, tmp, tcase ; tcase = 0 ; while (scanf ("%s", str) != EOF) { can = 1 ; stkPtr = 1 ; root = (NODE *) calloc (1, sizeof (NODE)) ; insert (str) ; while (scanf ("%s", str), *str != '9') insert (str) ; if (can) printf ("Set %d is immediately decodable\n", ++tcase) ; else printf ("Set %d is not immediately decodable\n", ++tcase) ; recycle () ; } return 0 ; }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值