首页 > 编程 > C++ > 正文

[华为OJ--C++]026-字符串加密

2019-11-08 03:20:08
字体:
来源:转载
供稿:网友

题目描述:

有一种技巧可以对数据进行加密,它使用一个单词作为它的密匙。下面是它的工作原理:首先,选择一个单词作为密匙,如TRAILBLAZERS。如果单词中包含有重复的字母,只保留第1个,其余几个丢弃。现在,修改过的那个单词死于字母表的下面,如下所示:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

T R A I L B Z E S C D F G H J K M N O P Q U V W X Y

上面其他用字母表中剩余的字母填充完整。在对信息进行加密时,信息中的每个字母被固定于顶上那行,并用下面那行的对应字母一一取代原文的字母(字母字符的大小写状态应该保留)。因此,使用这个密匙,Attack AT DAWN(黎明时攻击)就会被加密为TPPTad TP ITVH。

输入描述:输入key和要加密的字符串

输出描述:返回加密后的字符串

输入实例:

nihao

 ni

输出实例:

le

算法实现:

#include<iostream>#include<string>#include<vector>using namespace std;/************************************************  * Author: 赵志乾  * Date: 2017-2-17   * Declaration: All Rigths Reserved !!!  ***********************************************/ int main(){	string key,instr;	cin>>key>>instr;	vector<char>code(26,0);	vector<char>state(26,0);	int count=0;	for(int i=0;i<key.length();i++)	{		int index=key[i]>='a'?'a':'A';		index=key[i]-index;		if(state[index]==0)		{			state[index]=1;			code[count]=index;			count++;		}	}	for(int i=0;i<26;i++)	{		if(state[i]==0)		{			state[i]=1;			code[count]=i;			count++;		}	}	string ret=instr;	for(int i=0;i<instr.length();i++)	{		char offset=instr[i]>='a'?'a':'A';		ret[i]=code[instr[i]-offset]+offset;	}		cout<<ret<<endl;	return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选