1474:Immediate Decodability

【题目描述】

原题来自:ACM Pacific NW Region 1998

给出一些数字串,判断是否有一个数字串是另一个串的前缀。
【输入】

输入数据为多组数据,每组数据读到 9 时结束。
【输出】

对于每组数据,如果不存在一个数字串是另一个串的前缀,输出一行 Set t is immediately decodable ,否则输出一行 Set t is not immediately decodable ,其中 t 是这一组数据的组号。
【输入样例】

01
10
0010
0000
9
01
10
010
0000
9

【输出样例】

Set 1 is immediately decodable
Set 2 is not immediately decodable

【提示】

数字串只包含 0,1,记每个数字串长度为 l,则 1≤l≤10。每组数据至少有 2 个数字串,至多有 8 个数字串。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N=100000,Z=20;

int son[N][Z],idx;
bool vis[N];
string str;
int n;
bool flag;
int t;

void clear()
{
	memset(son,0,sizeof(son));
	memset(vis,0,sizeof(vis));
	idx=0;
	flag=0;
}

bool insert(string s)
{
	int p=0;
	bool flag=0;
	int len=s.size();
	for(int i=0;s[i];i++)
	{
		int u=s[i]-'0';
		if(!son[p][u]) son[p][u]=++idx;
		else if(i==len-1) flag=1;
		p=son[p][u];
		if(vis[p]) flag=1;
	}
	vis[p]=1;
	return flag;
}
int main()
{
	while(cin>>str)
	{
		if(str=="9")
		{	
			printf("Set %d is ",++t);
			if(flag)
			{
				printf("not immediately decodable\n");
			}
			else printf("immediately decodable\n");
			clear();
			continue;
		}
		if(insert(str)) flag=1;
	}
}
posted @ 2021-07-04 21:06  lyc_lb  阅读(197)  评论(0)    收藏  举报
-->
查看放大的地图   |   获取路线