Trie树 POJ1056 IMMEDIATE DECODABILITY

题目:http://poj.org/problem?id=1056

题意:判断是否有串是其他串的前缀

#include<algorithm>

#include<stdio.h>

#include<iostream>

#include<set>

#include<map>

#include<queue>

#include<stack>

#include<string.h>



using namespace std;



struct Node

{

    int cnt;Node * next[50];

}e[111111];



int len=0;



Node * newnode()

{

    memset(&e[len],0,sizeof(e[len]));

    return &e[len++];

}



void Insert(char *s,Node *root)

{

    int len=strlen(s);

    for(int i=0;i<len;i++){

        int cc=s[i]-'0';

        if(!root->next[cc]){

            root->next[cc]=newnode();

        }

        root->cnt++;

        root=root->next[cc];

    }

    root->cnt++;

}



int Find(char *s,Node *root)

{

    int len=strlen(s);

    for(int i=0;i<len;i++){

        int cc=s[i]-'0';

        root=root->next[cc];

    }

    if(root->cnt>1)

    return 1;

    else

        return 0;

}

char str[1005][1005];

int main()

{

    int i=-1;int ret=1;int flag=0;Node *root=newnode();

    while(scanf("%s",&str[++i])!=EOF){

        Insert(str[i],root);

        if(strcmp(str[i],"9")==0){

            for(int j=0;j<i;j++){

                if(Find(str[j],root)){

                    flag=1;

                    printf("Set %d is not immediately decodable\n",ret++);break;

                }

            }

            if(flag==0){

                printf("Set %d is immediately decodable\n",ret++);

            }

            i=-1;len=0;flag=0;root=newnode();

        }

    }

    return 0;

}

  

你可能感兴趣的:(media)