zoukankan      html  css  js  c++  java
  • Pj Immediate Decodability

    判断一个串是否是其他的前缀

    我们需要建立一颗tire树

    在插入边的时候,如果遇到一个其他串的结尾,那么就说明至少有一个串,是插入串的前缀。如果在插入完后没有新增的节点,那么插入的串就是其他串的前缀

    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    using namespace std;
    const int manx=1<<8;
    char data[manx];
    int t[manx][2],tail;
    int end[manx<<8];
    bool flag=false;
    void ins()
    {
    	int last=tail,now=0;
    	int len=strlen(data)-1;
    	for(int i=0;i<=len;i++)
    	{
    		int nxt=data[i]-'0';
    		if(!t[now][nxt])	t[now][nxt]=++tail;
    		if(end[now])	flag=true;//经过一个串的结尾
    		now=t[now][nxt];
    	}
    	end[now]+=1;
    	if(last==tail)	flag=true;//是其他串的前缀
    	return ;
    }
    int main()
    {
    	int tot=0;
    	//freopen("a.in","r",stdin);
    	while(scanf("%s",data)!=EOF)
    	{
    		if(data[0]=='9')
    		{
    			tot+=1;
    			if(!flag)
    				printf("Set %d is immediately decodable
    ",tot);
    			else
    			{
    				printf("Set %d is not immediately decodable
    ",tot);
    			}//每一组数据都要初始化
    			flag=false;
    			memset(t,0,sizeof(t));
    			memset(end,0,sizeof(end));
    			tail=0;
    			continue;
    		}
    		if(!flag)	ins();
    	}
    }
    
  • 相关阅读:
    [背包]JZOJ 3232 【佛山市选2013】排列
    内核空间、用户空间、虚拟地址
    进程与线程的概念
    Python中字符串颜色
    socket编程
    模块与包
    常用模块
    面向对象进阶
    面向对象编程
    函数式编程
  • 原文地址:https://www.cnblogs.com/Lance1ot/p/9225816.html
Copyright © 2011-2022 走看看