SRM 668 DIV 2 VerySecureEncryption 250point

本文介绍了一种名为VerySecureEncryption的安全加密方法,该方法通过多次使用特定的键向量来混淆原始消息,达到加密的目的。文章提供了加密算法的具体实现,并通过几个实例展示了不同参数设置下加密过程的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Statement

You are going to send a message to your friend. The message is given as the string message. To confuse potential eavesdroppers, you are going to scramble the message. Scrambling of a message is performed using the vector key. If a letter is at the (0-based) position i in the original message, it will appear at the position key[i] in the scrambled message. (The constraints given below guarantee that this process will produce a valid scrambled message.) To make the encryption even more confusing, you are going to repeat the above process K times in a row. Given message, key, and the int K, find and return the final encrypted message.
Definition

Class:
VerySecureEncryption
Method:
encrypt
Parameters:
string, vector , int
Returns:
string
Method signature:
string encrypt(string message, vector key, int K)
(be sure your method is public)
Limits

Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256

Constraints

N will be between 1 and 10, inclusive.

message will contain N characters.

Each character of message will be a lowercase English letter.

key will contain N elements.

Each element of key will be between 0 and N-1, inclusive.

The elements of key will be distinct.

K will be between 1 and 50, inclusive.
Examples
0)

“abc”
{1,2,0}
1
Returns: “cab”
The character ‘a’ will go from position 0 to position key[0]=1.
The character ‘b’ will go from position 1 to position key[1]=2.
The character ‘c’ will go from position 2 to position key[2]=0.
1)

“abcde”
{4, 3, 2, 1, 0}
1
Returns: “edcba”

2)

“abcde”
{4, 3, 2, 1, 0}
2
Returns: “abcde”
This is the same message and the same key as in example 1, but now K=2, so we scramble the message twice. For this particular key we see that each scrambling reverses the order of letters, which means that the final message is the same as the original we started with.
3)

“uogcodlk”
{4, 3, 6, 2, 5, 1, 0, 7}
44
Returns: “goodluck”

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

My Solution

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

class VerySecureEncryption
{
    public:
        string encrypt(string message, vector <int> key, int K);
};

string VerySecureEncryption::encrypt(string message, vector <int> key, int K)
{
    string ans = message;
    int size = message.size();

    for (int k = 0; k < K; k++)
    {
        string orgStr = ans;
        for (int i = 0; i < size; ++i)
        {
            ans[key[i]] = orgStr[i];
        }
    }

    return ans;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值