Immediate Decodability

https://loj.ac/problem/10052

Title Description

  It gives some string, if there is a request string is a prefix of another string.

Thinking

  And Phone List exactly the same, the dictionary tree template title, but different output, you can initialize the attention, it does not go into details.

Code

#include <bits/stdc++.h>
using namespace std;
int ch[5000][3],tot;
bool ed[5000];
void clear()
{
    memset(ch,0,sizeof(ch));
    memset(ed,0,sizeof(ed));
    to = 1 ;
}
bool insert(char *s)
{
    int u=1;
    int len=strlen(s);
    bool f=0;
    for(int i=0;i<len;i++)
    {
        int num=s[i]-'0';
        if(!ch[u][num])ch[u][num]=++tot;
        else if(i==len-1)f=1;
        u=ch[u][num];
        if(ed[u])f=1;
    }
    ed[u]=1;
    return f;
}
int main ()
{
    int cas=0;
    char s[20];
    while(~scanf(" %s",s))
    {
        bool f=0;
        clear();
        if(insert(s))f=1;
        while(scanf(" %s",s))
        {
            if(strlen(s)==1&&s[0]=='9')break ;
            if(insert(s))f=1;
        }
        if(!f)printf("Set %d is immediately decodable\n",++cas);
        else printf("Set %d is not immediately decodable\n",++cas);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/fangbozhen/p/11628494.html