[CKOJ] ROUND 7 Problem B 加密与解密

问题 B: 加密与解密

时间限制: 1 Sec  内存限制: 128 MB
提交: 44  解决: 19
[提交][状态][讨论版]

题目描述

给定字符串a,b并定义如下加密方式

不断用b对a依照下表按位转换

当b长度小于a时重复使用b即可

输入数据

  • 第一行一个整数T表示数据组数
  • 接下来T行每行一个整数o,两个字符串a和b,用空格隔开
  • 当o为1时代表a为原码,o为2时代表a为密码

输出数据

  • 共T行,每行一个字符串代表加密前/后的字符串
  • 输出全部使用小写字母

样例输入

  • 3
  • 1 abyn cx
  • 1 zdwisretarded gg
  • 2 ooooooo afk

样例输出

  • cyak
  • fjcoyxkzgxjkj
  • ojeojeo

输入

输出

提示

对于40%的数据, 保证所有o为1(只需处理加密)

对于100%的数据, 保证T<=100, 1<=a,b长度<=1000, 1<=o<=2

白想了很多 其实暴力穷举打表是最快最直观的选择

节约时间提升效率

#include <iostream>
using namespace std;
int main()
{
	string stl[26]={
	"abcdefghijklmnopqrstuvwxyz",
	"bcdefghijklmnopqrstuvwxyza",
	"cdefghijklmnopqrstuvwxyzab",
	"defghijklmnopqrstuvwxyzabc",
	"efghijklmnopqrstuvwxyzabcd",
	"fghijklmnopqrstuvwxyzabcde",
	"ghijklmnopqrstuvwxyzabcdef",
	"hijklmnopqrstuvwxyzabcdefg",
	"ijklmnopqrstuvwxyzabcdefgh",
	"jklmnopqrstuvwxyzabcdefghi",
	"klmnopqrstuvwxyzabcdefghij",
	"lmnopqrstuvwxyzabcdefghijk",
	"mnopqrstuvwxyzabcdefghijkl",
	"nopqrstuvwxyzabcdefghijklm",
	"opqrstuvwxyzabcdefghijklmn",
	"pqrstuvwxyzabcdefghijklmno",
	"qrstuvwxyzabcdefghijklmnop",
	"rstuvwxyzabcdefghijklmnopq",
	"stuvwxyzabcdefghijklmnopqr",
	"tuvwxyzabcdefghijklmnopqrs",
	"uvwxyzabcdefghijklmnopqrst",
	"vwxyzabcdefghijklmnopqrstu",
	"wxyzabcdefghijklmnopqrstuv",
	"xyzabcdefghijklmnopqrstuvw",
	"yzabcdefghijklmnopqrstuvwx",
	"zabcdefghijklmnopqrstuvwxy",
	};
	int N;
	cin>>N;
	while(N--)
	{
		int mode;
		cin>>mode;
		string a,b;
		cin>>a>>b;
		int lenb=b.length(),flag=0;
		int x,y;
		if(mode==1)
		{
			for(int i=0;i<a.length();i++)
			{
				x=a[i]-'a';
				y=b[flag]-'a';
				cout<<stl[x][y];
				flag++;
				if(flag==lenb)
					flag=0;
			}
		}
		else
		{
			for(int i=0;i<a.length();i++)
			{
				x=b[flag]-'a';
				for(int j=0;j<26;j++)
				{
					if(stl[x][j]==a[i])
					{
						char ans=j+'a';
						cout<<ans;
						break;
					}
				}
				flag++;
				if(flag==lenb)
					flag=0;
			}
		}
		cout<<endl;
	}
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Hill 加密解密的 Python 代码示例: ```python import numpy as np # 定义明文转换为数字序列的函数 def text_to_numbers(text): numbers = [] for c in text: if c.isalpha(): numbers.append(ord(c.lower()) - ord('a')) return numbers # 定义数字序列转换为明文的函数 def numbers_to_text(numbers): text = "" for n in numbers: text += chr(n + ord('a')) return text # 定义 Hill 加密函数 def hill_encrypt(plaintext, key): n = len(key) plaintext = plaintext.lower().replace(" ", "") plaintext_numbers = text_to_numbers(plaintext) # 若数字序列长度不是密钥矩阵大小的整数倍,则补全 while len(plaintext_numbers) % n != 0: plaintext_numbers.append(0) plaintext_matrix = np.array(plaintext_numbers).reshape(-1, n) # 加密 ciphertext_matrix = (plaintext_matrix @ key) % 26 ciphertext_numbers = ciphertext_matrix.flatten().tolist() ciphertext = numbers_to_text(ciphertext_numbers) return ciphertext # 定义 Hill 解密函数 def hill_decrypt(ciphertext, key): n = len(key) ciphertext = ciphertext.lower().replace(" ", "") ciphertext_numbers = text_to_numbers(ciphertext) ciphertext_matrix = np.array(ciphertext_numbers).reshape(-1, n) # 计算解密矩阵 det = int(round(np.linalg.det(key))) inv_key = np.linalg.inv(key) inv_det = pow(det, -1, 26) adj_key = np.round(inv_det * det * inv_key).astype(int) % 26 # 解密 plaintext_matrix = (ciphertext_matrix @ adj_key) % 26 plaintext_numbers = plaintext_matrix.flatten().tolist() plaintext = numbers_to_text(plaintext_numbers) return plaintext # 测试代码 plaintext = "HELLO WORLD" key = np.array([[3, 2], [5, 7]]) ciphertext = hill_encrypt(plaintext, key) print("Ciphertext:", ciphertext) decrypted_text = hill_decrypt(ciphertext, key) print("Decrypted text:", decrypted_text) ``` 以上代码中,`text_to_numbers()` 函数用于将明文转换为数字序列,`numbers_to_text()` 函数用于将数字序列转换为明文。`hill_encrypt()` 函数接受一个明文和一个密钥矩阵,返回加密后的密文;`hill_decrypt()` 函数接受一个密文和一个密钥矩阵,返回解密后的明文。在加密解密时,只对字母进行加密解密操作,其他字符不变。 需要注意的是,Hill 加密算法要求密钥矩阵的行列式必须是模 26 意义下的可逆元。若密钥矩阵不符合要求,则无法正确解密。在本代码示例中,我们计算密钥矩阵的行列式和逆矩阵时,使用了 numpy 库中的相应函数,以确保计算的正确性。如果没有安装 numpy 库,可以使用以下命令进行安装: ```python pip install numpy ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值