Mapping Cipher

题目: 输入字母表中所映射的密文, 再将你输入的原文按照对应的密文所转换后输出。(加密)

        Mapping Cipher
You are given an alphabetic mapping table to create cipher text. The table is defined
by a string using 26 lower-case alphabets, such as
incpfvztsmbexowhlgkqjyrdau
In the string all characters are different. Using this table to generate cipher text, it
performs the following mapping scheme:
a -> i, A->I
b -> n, B->N
c -> c, C->C
d -> p, D->P
. . . . . . .
y -> a, Y->A
z -> u, Z->U
Any non-alphabetic character are kept unchanged.
Apply the scheme to plain text:
There are 2 dogs and 3 cats.
It rains dogs & cats!
Obtain the following cipher text:
Qtfgf igf 2 pwzk iop 3 ciqk.
Sq gisok pwzk & ciqk!

Input
There are several test cases. Each test case starts with a line of mapping string
described above. Following are plain text enclosed by two lines of string "***".
Your task is to output the cypher text encrypted using the given mapping string. There
is a blank line after each case. Finally, you will reach a line of string "###" which
signals the end of test. Each line in the test case is no more than 1000 characters. Each
line in test cases is no more than 1000 characters.

Output
The cipher text of each test case is prefixed with a cipher text number as shown in the
sample output. Place a blank line between cases.

Sample Input
incpfvztsmbexowhlgkqjyrdau
***
There are 2 dogs and 3 cats.
It rains dogs & cats!
***
xowhlgkqjyrdauincpfvztsmbe
***
There are 2 dogs and 3 cats.
It rains dogs & cats!
***
###

Sample Output
Cypher 1:
Qtfgf igf 2 pwzk iop 3 ciqk.
Sq gisok pwzk & ciqk!
Cypher 2:
Vqlpl xpl 2 hikf xuh 3 wxvf.
Jv pxjuf hikf & wxvf!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	int flag=1, test=1;
	
	while(1)
	{
		char arr[26]={0}; //存取字母映射的密文
		char z[10]={0}; //存取密文后的***
		char str[1001]={0}; //存取输入的原文
		
		gets(arr);
		if(arr[0]=='#' && arr[1]=='#' && arr[2]=='#') break;
		
		gets(z);
		
		if(flag) flag=0;
		else printf("\n");
		
		printf("Cypher %d:\n",test++);
		
		while(gets(str))
		{
			if(str[0]=='*' && str[1]=='*' && str[2]=='*') break;
			
			for(int i=0; i<strlen(str); i++)
			{
				if(str[i]>='a' && str[i]<='z') printf("%c",arr[str[i]-'a']);
				else if(str[i]>='A' && str[i]<='Z') printf("%c",arr[str[i]-'A']+'A'-'a');
				else printf("%c",str[i]);
			}
			
			printf("\n");
		}
		
		scanf("\n");
			
	}
    
    return 0;
}



将一下python代码进行逐句分析,并详细说出为什么这么做# 标准英文频率 std_freq = [ 8.167, 1.492, 2.782, 4.253, 12.702, 2.228, 2.015, 6.966, 7.507, 0.153, 0.747, 4.025, 2.406, 6.749, 7.507, 1.929, 0.095, 5.987, 6.327, 9.056, 2.758, 0.978, 2.360, 0.150, 1.974, 0.074 ] # 将每个字母出现的频率进行,标准频率排名 std_rank = [4, 19, 0, 8, 14, 2, 18, 3, 5, 13, 11, 1, 15, 6, 17, 10, 20, 7, 9, 24, 22, 21, 25, 12, 16, 23] #密文中出现的字母及频率 def analyze_cipher(cipher): count = [0] * 26 total = 0 for char in cipher: if char.isalpha(): c = char.upper() count[ord(c) - ord('A')] += 1 total += 1 freq = [count[i] * 100.0 / total if total else 0 for i in range(26)] return count, freq #密文中出现字母频率的排名 def get_rank(freq): rank = [0] * 26 for i in range(26): for j in range(26): if freq[j] > freq[i]: rank[i] += 1 return rank #初始映射表 def init_mapping(cipher_rank): mapping = [None] * 26 used = [False] * 26 # 优先匹配高频字母(前6位:E,T,A,O,I,N) for i in range(6): for c in range(26): if cipher_rank[c] == i: for p in range(26): if std_rank[p] == i and not used[p]: mapping[c] = chr(ord('A') + p) used[p] = True break break # 填充剩余字母 for c in range(26): if mapping[c] is None: for p in range(26): if not used[p]: mapping[c] = chr(ord('A') + p) used[p] = True break return mapping #使用映射表进行解密 def decrypt(cipher, mapping): plain = [] for char in cipher: if char.isalpha(): c = char.upper() new_char = mapping[ord(c) - ord('A')] plain.append(new_char.lower() if char.islower() else new_char) else: plain.append(char) return ''.join(plain) #打印密文的频率和标准英文的频率 def print_analysis(count, freq, cipher_rank): print("\n===== 密文频率分析 =====") print("字母 | 出现次数 | 频率(%) | 排名") print("-------------------------") for i in range(26): print(f" {chr(ord('A') + i)} | {count[i]:5d} | {freq[i]:5.2f} | {cipher_rank[i]:2d}") print("\n===== 标准英文频率排名 =====") print("高频字母(前6位): E(12.7%) > T(9.1%) > A(8.2%) > O(7.5%) > I(7.0%) > N(6.7%)") #主函数 cipher = input("请输入密文: ") count, freq = analyze_cipher(cipher) cipher_rank = get_rank(freq) print_analysis(count, freq, cipher_rank) mapping = init_mapping(cipher_rank) plain = decrypt(cipher, mapping) while True: print(f"\n当前解密结果\n{plain}") cmd = input("\n映射表调整 (格式: 密文 明文,如 'X E';输入q退出): ") if cmd.lower() == 'q': break if len(cmd) >= 3 and cmd[0].isalpha() and cmd[2].isalpha(): c = ord(cmd[0].upper()) - ord('A') p = ord(cmd[2].upper()) - ord('A') # 处理冲突 for i in range(26): if mapping[i] == chr(ord('A') + p): for j in range(26): if chr(ord('A') + j) not in mapping: mapping[i] = chr(ord('A') + j) break break mapping[c] = chr(ord('A') + p) plain = decrypt(cipher, mapping) else: print("输入格式错误,请重试!") print(f"\n最终解密结果\n{plain}")
10-16
分析修改一下以下python代码并给出修改后的完整代码#include <stdio.h> #include <string.h> #include <ctype.h> #include <math.h> const float std_freq[26] = { # type: ignore 8.167, 1.492, 2.782, 4.253, 12.702, 2.228, 2.015, 6.966, 7.507, 0.153, 0.747, 4.025, 2.406, 6.749, 7.507, 1.929, 0.095, 5.987, 6.327, 9.056, 2.758, 0.978, 2.360, 0.150, 1.974, 0.074 }; void analyze_cipher(const char *cipher, int count[26], float freq[26]) { # type: ignore int total = 0; memset(count, 0, 26*sizeof(int)); //统计每个字母出现的次数// for (int i = 0; cipher[i]; i++) { if (isalpha(cipher[i])) { char c = toupper(cipher[i]); count[c-'A']++; total++; } } //计算每个字母出现的频率// for (int i = 0; i < 26; i++) { freq[i] = total ? (count[i] * 100.0f / total) : 0; } } void get_rank(const float freq[26], int rank[26]) { # type: ignore for (int i = 0; i < 26; i++) { rank[i] = 0; for (int j = 0; j < 26; j++) { if (freq[j] > freq[i]) rank[i]++; } } } void init_mapping(const int cipher_rank[26], char mapping[26]) { # type: ignore // 标准频率排名 int std_rank[26] = {4,19,0,8,14,2,18,3,5,13,11,1,15,6,17,10,20,7,9,24,22,21,25,12,16,23}; // 临时数组记录已使用的明文字母 int used[26] = {0}; // 优先匹配高频字母(前6位:E,T,A,O,I,N) for (int i = 0; i < 6; i++) { // 找到密文中排名第i的字母 for (int c = 0; c < 26; c++) { if (cipher_rank[c] == i) { // 匹配标准排名第i的字母 for (int p = 0; p < 26; p++) { if (std_rank[p] == i && !used[p]) { mapping[c] = 'A' + p; used[p] = 1; break; } } break; } } } //计算频率的排名// void get_rank(const float freq[26], int rank[26]) { for (int i = 0; i < 26; i++) { rank[i] = 0; for (int j = 0; j < 26; j++) { if (freq[j] > freq[i]) rank[i]++; } } } //填充剩余字母// for (int c = 0; c < 26; c++) { if (!mapping[c]) { // 未映射的密文字母 for (int p = 0; p < 26; p++) { if (!used[p]) { mapping[c] = 'A' + p; used[p] = 1; break; } } } } } //使用映射表解密// void decrypt(const char *cipher, const char mapping[26], char *plain) { for (int i = 0; cipher[i]; i++) { if (isalpha(cipher[i])) { char c = toupper(cipher[i]); plain[i] = islower(cipher[i]) ? tolower(mapping[c-'A']) : mapping[c-'A']; } else { plain[i] = cipher[i]; // 保留非字母字符 } } plain[strlen(cipher)] = '\0'; } //显示频率分析结果// void print_analysis(const int count[26], const float freq[26], const int rank[26]) { printf("\n===== 密文频率分析 =====\n"); printf("字母 | 出现次数 | 频率(%%) | 排名\n"); printf("-------------------------\n"); for (int i = 0; i < 26; i++) { printf(" %c | %5d | %5.2f | %2d\n", 'A'+i, count[i], freq[i], rank[i]); } printf("\n===== 标准英文频率排名 =====\n"); printf("高频字母(前6位): E(12.7%%) > T(9.1%%) > A(8.2%%) > O(7.5%%) > I(7.0%%) > N(6.7%%)\n"); } int main() { char cipher[10000], plain[10000]; int count[26], cipher_rank[26]; float freq[26]; char mapping[26] = {0}; // 密文→明文映射表(索引=密文'A'-'Z') // 输入密文// printf("请输入密文: "); fgets(cipher, sizeof(cipher), stdin); cipher[strcspn(cipher, "\n")] = '\0'; // 移除换行符 //频率分析// analyze_cipher(cipher, count, freq); get_rank(freq, cipher_rank); print_analysis(count, freq, cipher_rank); //生成初始映射并解密// init_mapping(cipher_rank, mapping); decrypt(cipher, mapping, plain); //交互式调整映射// char cmd[10]; while (1) { printf("\n===== 当前解密结果 =====\n%s\n", plain); printf("\n映射表调整 (格式: 密文 明文,如 'X E';输入q退出): "); fgets(cmd, sizeof(cmd), stdin); cmd[strcspn(cmd, "\n")] = '\0'; if (cmd[0] == 'q' || cmd[0] == 'Q') break; // 解析调整命令(如"X E"表示密文X→明文E)// if (strlen(cmd)>=3 && isalpha(cmd[0]) && isalpha(cmd[2])) { char c = toupper(cmd[0]) - 'A'; // 密文字母索引 char p = toupper(cmd[2]) - 'A'; // 明文字母索引 // 处理冲突:如果p已被其他字母映射,先释放 for (int i = 0; i < 26; i++) { if (mapping[i] == 'A' + p) { // 为被占用的字母分配新的未使用字母 for (int j = 0; j < 26; j++) { int used = 0; for (int k = 0; k < 26; k++) { if (mapping[k] == 'A' + j) { used = 1; break; } } if (!used) { mapping[i] = 'A' + j; break; } } break; } } //更新映射// mapping[c] = 'A' + p; decrypt(cipher, mapping, plain); // 重新解密 } else { printf("输入格式错误,请重试!\n"); } } //输出最终结果// printf("\n===== 最终解密结果 =====\n%s\n", plain); return 0; }
10-16
【路径规划】(螺旋)基于A星全覆盖路径规划研究(Matlab代码实现)内容概要:本文围绕“基于A星算法的全覆盖路径规划”展开研究,重点介绍了一种结合螺旋搜索策略的A星算法在栅格地图中的路径规划实现方法,并提供了完整的Matlab代码实现。该方法旨在解决移动机器人或无人机在未知或部分已知环境中实现高效、无遗漏的区域全覆盖路径规划问题。文中详细阐述了A星算法的基本原理、启发式函数设计、开放集与关闭集管理机制,并融合螺旋遍历策略以提升初始探索效率,确保覆盖完整性。同时,文档提及该研究属于一系列路径规划技术的一部分,涵盖多种智能优化算法与其他路径规划方法的融合应用。; 适合人群:具备一定Matlab编程基础,从事机器人、自动化、智能控制及相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于服务机器人、农业无人机、扫地机器人等需要完成区域全覆盖任务的设备路径设计;②用于学习和理解A星算法在实际路径规划中的扩展应用,特别是如何结合特定搜索策略(如螺旋)提升算法性能;③作为科研复现与算法对比实验的基础代码参考。; 阅读建议:建议结合Matlab代码逐段理解算法实现细节,重点关注A星算法与螺旋策略的切换逻辑与条件判断,并可通过修改地图环境、障碍物分布等方式进行仿真实验,进一步掌握算法适应性与优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值