[数据结构]前缀码判定 解题报告

2017-03-26  本文已影响0人  vouv

Problem Description

前缀码:任何一个字符的编码都不是同一字符集中另一个字符的编码的前缀。

请编写一个程序,判断输入的n个由1和0组成的编码是否为前缀码。如果这n个编码是前缀码,则输出"YES”;否则输出第一个与前面编码发生矛盾的编码。

输入:

第1行为n(表示下面有n行编码)
第2~n+1行为n个由0或1组成的编码

输出:判断结果

例如,如果输入:

5
00
01
10
110
111

每一个字符均不是其他字符编码的前缀,所以,输出:

YES

再如,如果输入:

5
00
01
10
110
11

编码11与前面的编码110的前缀,所以,输出:

11


测试输入

5
00
01
10
110
111

测试输出

YES

AcCode

//
//  main.cpp
//  前缀码判定
//
//  Created by jetviper on 2017/3/26.
//  Copyright © 2017年 jetviper. All rights reserved.
//

#include <stdio.h>
#include<stdlib.h>
#include <string.h>
typedef  struct {
    unsigned  int lchild=0,rchild=0,tail=0;
}HTNode,*HTT;
char str[100000];
HTNode ht[100000];
int main() {
    // HTT ht;
    int n;
    scanf("%d",&n);
    int i,j,k=2;
    int err =0,now;
    
    for(i =0;i<n;i++){
        
        scanf("%s",str);
        
        if(err==1)continue;
        
        int len = strlen(str);
        
        //start searching
        now = 1;
        
        for(j=0;j<len;j++){
            
            if(ht[now].tail==1){
                err=1;
                printf("%s\n",str);
                break;
            }
            
            
            //if not the last
            
            if(j<len-1){
                
                
                
                //left
                if(str[j]=='0'){
                    
                    if(ht[now].lchild==0){
                        ht[now].lchild = k;
                        now = k;
                        k++;
                        continue;
                    }
                    else {
                        now = ht[now].lchild;
                        continue;
                    }
                    
                }
                //leftend
                
                //right
                else {
                    
                    if(ht[now].rchild==0){
                        ht[now].rchild = k;
                        now = k;
                        k++;
                        continue;
                    }
                    else {
                        now = ht[now].rchild;
                        continue;
                    }
                    //rightend
                }
                
                
                
            }
            //else the last
            else {
                
                if(str[j]=='0'){
                    
                    if(ht[now].lchild==0){
                        ht[now].lchild = k;
                        ht[k].tail=1;
                        k++;
                        continue;
                    }
                    else {
                        now = ht[now].lchild;
                        if(ht[now].lchild ==0&&ht[now].rchild==0){
                            ht[now].tail = 1 ;
                        }
                        else{
                            err=1;
                            printf("%s\n",str);
                            break;
                            
                        }
                    }
                }
                //righttail
                else {
                    
                    if(ht[now].rchild==0){
                        ht[now].rchild = k;
                        ht[k].tail=1;
                        k++;
                        continue;
                    }
                    else {
                        now = ht[now].rchild;
                        if(ht[now].lchild ==0&&ht[now].rchild==0){
                            ht[now].tail = 1 ;
                            break;
                        }
                        else{
                            err=1;
                            printf("%s\n",str);
                            break;
                            
                        }
                    }
                }
            }
        }
    }
    if(err==0)printf("YES\n");
    
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读