w15作业--ZJM 与生日礼物(选做)

题意:

ZJM 收到了 Q老师 送来的生日礼物,但是被 Q老师 加密了。只有 ZJM 能够回答对 Q老师 的问题,Q老师 才会把密码告诉 ZJM。
Q老师 给了 ZJM 一些仅有 01 组成的二进制编码串, 他问 ZJM:是否存在一个串是另一个串的前缀.

输入格式:
多组数据。每组数据中包含多个仅有01组成的字符串,以一个9作为该组数据结束的标志。

输出格式:
对于第 k 组数据(从1开始标号),如果不存在一个字符串使另一个的前缀,输出"Set k is immediately decodable",否则输出"Set k is not immediately decodable"。

每组数据的输出单独一行

样例输入:

01
10
0010
0000
9
01
10
010
0000
9

样例输出:

Set 1 is immediately decodable
Set 2 is not immediately decodable

思路:

(1)题意分析:一个字符串和一个字符串集合匹配,因此考虑使用字典树
先记录一下字典树的模板:

struct Trie{
	static const int N=1010,num=2;
	int tot,root,child[N][num],flag[N];
	Trie(){
		memset(child,-1,sizeof(child));
		root=tot=0;
	}
	void init(){
		memset(child,-1,sizeof(child));
		root=tot=0;
	}
	void insert(string s){
		int now=root;
		bool jud=false;
		for(int i=0;s[i];i++){
			int x=s[i]-'a';
			if(child[now][x]==-1){
				child[now][x]=++tot;
				flag[now]=0;
			}

			now=child[now][x];
		}
		flag[now]=1;

	}
	bool query(string s){
		int now=root;
		for(int i=0;s[i];i++){
			int x=s[i]-'a';
			if(child[now][x]==-1) return false;
			if(flag[now]) return true;
			now=child[now][x];

		}
		return false;
	}
};

(2)本题做法:
往字典树中依次插入每个字符串,假设当前字符串为 S
有两种情况需要判断:
S 是否为之前某个字符串的前缀
之前是否有某个字符串是 S 的前缀

对于 S 是否为之前某个字符串的前缀 :
当字符串 S 插入结束后,其最后一个节点是字典树中已经存在的节点,则说明 S 为之前某个字符串的前缀
对于之前是否有某个字符串是 S 的前缀:
在字符串 S 插入过程中,如果遇到某一个字典树中的节点为某一个字符串的结尾,则说明存在某个字符串是 S 的前缀

这里修改一下上述模板中的insert代码即可

bool insert(string s){
		int now=root,l=s.size();
		bool jud=false;
		for(int i=0;i<l;i++){
			int x=s[i]-'0';
			if(child[now][x]==-1){
				child[now][x]=++tot;
				flag[now]=0;
			}
			else if(i==l-1||flag[child[now][x]]) jud=true;
			now=child[now][x];
		}
		flag[now]=1;
		return jud;
	}

代码:

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

struct Trie{
	static const int N=1010,num=2;
	int tot,root,child[N][num],flag[N];
	Trie(){
		memset(child,-1,sizeof(child));
		root=tot=0;
	}
	void init(){
		memset(child,-1,sizeof(child));
		root=tot=0;
	}
	bool insert(string s){
		int now=root,l=s.size();
		bool jud=false;
		for(int i=0;i<l;i++){
			int x=s[i]-'0';
			if(child[now][x]==-1){
				child[now][x]=++tot;
				flag[now]=0;
			}
			else if(i==l-1||flag[child[now][x]]) jud=true;
			now=child[now][x];
		}
		flag[now]=1;
		return jud;
	}
};
int k=1;string str;Trie t;

int main(){
	while(cin>>str){
		if(str=="9") continue;
		t.init();
		bool j=t.insert(str);
		int fflag=0;
		while(cin>>str){
			if(str=="9") break;
			if(t.insert(str)) fflag=1;
		}
		if(fflag) cout<<"Set "<<k<<" is not immediately decodable"<<endl;
		else cout<<"Set "<<k<<" is immediately decodable"<<endl;
		k++;
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,作为AI语言模型,我不能编写代码,但我可以为您提供SHA-0算法的伪代码: // SHA-0算法 // 输入:消息字符串message // 输出:160位摘要值digest 1. 初始化5个32位寄存器H0、H1、H2、H3、H4 H0 ← 67452301 H1 ← EFCDAB89 H2 ← 98BADCFE H3 ← 10325476 H4 ← C3D2E1F0 2. 对消息进行填充,使其长度为512的整倍 message ← message + 1 while (message.length mod 512 != 448) do message ← message + 0 end while message ← message + length_of_message_in_bits 3. 将消息分割成512位的块M1、M2、…、Mn for i from 1 to n do // 将512位块Mi分成16个32位的字W0、W1、…、W15 for j from 0 to 15 do Wj ← Mi[j*32+1:(j+1)*32] end for // 扩展16个字为80个字 for j from 16 to 79 do Wj ← W(j-3) xor W(j-8) xor W(j-14) xor W(j-16) Wj ← (Wj << 1) or (Wj >> 31) end for // 初始化5个临时变量A、B、C、D、E A ← H0 B ← H1 C ← H2 D ← H3 E ← H4 // 迭代计算5个临时变量的值 for j from 0 to 79 do if (0 <= j <= 19) then f ← (B and C) or ((not B) and D) k ← 0x5A827999 else if (20 <= j <= 39) then f ← B xor C xor D k ← 0x6ED9EBA1 else if (40 <= j <= 59) then f ← (B and C) or (B and D) or (C and D) k ← 0x8F1BBCDC else if (60 <= j <= 79) then f ← B xor C xor D k ← 0xCA62C1D6 end if temp ← ((A << 5) or (A >> 27)) + f + E + k + Wj E ← D D ← C C ← (B << 30) or (B >> 2) B ← A A ← temp end for // 更新5个寄存器的值 H0 ← H0 + A H1 ← H1 + B H2 ← H2 + C H3 ← H3 + D H4 ← H4 + E end for 4. 将5个寄存器的值连接起来,得到160位的摘要值digest digest ← H0 || H1 || H2 || H3 || H4

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值