poj1056

简单题

#include <iostream>
#include <string>
using namespace std;

struct cnode
{
    cnode    *pzero, *pone;
    bool    end;
}trie[20000];

int        ncount, t;

bool insert(cnode *proot, char *a)
{
    if (proot->end)
        return false;
    if (a[0] == '1')
    {
        if (a[0] != '\0')
        {
            if (proot->pone == NULL)
            {
                ncount++;
                proot->pone = trie + ncount;
                proot->pone->end = false;
                proot->pone->pone = NULL;
                proot->pone->pzero = NULL;
            }
            return insert(proot->pone, a + 1);
        }
        proot->end = true;
        if (proot->pone == NULL && proot->pzero == NULL)
            return true;
        return false;
    }
    if (a[0] != '\0')
    {
        if (proot->pzero == NULL)
        {
            ncount++;
            proot->pzero = trie + ncount;
            proot->pzero->end = false;
            proot->pzero->pone = NULL;
            proot->pzero->pzero = NULL;
        }        
        return insert(proot->pzero, a + 1);
    }
    proot->end = true;
    if (proot->pone == NULL && proot->pzero == NULL)
        return true;
    return false;
}

void init()
{
    char*    st;
    bool    ans;

    st = "";
    ncount = 0;
    trie[0].end = false;
    trie[0].pzero = NULL;
    trie[0].pone = NULL;
    ans = true;
    while (true)
    {
        st = new char[11];
        if (!(cin >> st))
            exit(0);
        if (st[0] == '9')
            break;
        if (ans)
            if (!insert(trie, st))
                ans = false;
        getchar();
    }
    printf("Set %d is ", t);
    if (ans)
        printf("immediately decodable\n");
    else
        printf("not immediately decodable\n");
}

int main()
{
    //freopen("t.txt", "r", stdin);
    t = 0;
    while (true)
    {
        t++;
        init();
    }
    return 0;
}
View Code

 

posted @ 2013-07-04 15:28  金海峰  阅读(268)  评论(0编辑  收藏  举报