poj1056IMMEDIATE DECODABILITY(字典树)

->题目请戳这里<-

题目大意:跟这题基本一样

题目分析:略,练习一下徒手静态字典树,最后一道了。。

详情请见代码:

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

struct node
{
    int next[2];
    int tag,end;
}lcm[1<<16];
char s[20];
int flag;
int n;

void init(int i)
{
    memset(lcm[i].next,0,sizeof(lcm[i].next));
    lcm[i].tag = lcm[i].end = 0;
}

void build(int t,int id)
{
    if(lcm[t].end)
        flag = 1;
    if(flag)
        return;
    if(lcm[t].next[s[id] - '0'] == 0)
    {
        lcm[t].next[s[id] - '0'] = n;
        init(n);
        n ++;
    }
    if(s[id + 1] == '\0')
    {
        if(lcm[lcm[t].next[s[id] - '0']].tag == 1)
            flag = 1;
        lcm[lcm[t].next[s[id] - '0']].end = 1;
        return ;
    }
    else
        build(lcm[t].next[s[id] - '0'],id + 1);
}

int main()
{
    int i,cas = 0;
    flag = 0;
    init(0);
    n = 1;
    while(scanf("%s",s) != EOF)
    {
        if(s[0] == '9')
        {
            if(flag)
                printf("Set %d is not immediately decodable\n",++cas);
            else
                printf("Set %d is immediately decodable\n",++cas);
            flag = 0;
            init(0);
            n = 1;
        }
        else
            build(0,0);
    }
    return 0;
}
//176K	0MS


你可能感兴趣的:(数据结构,字典树)