【模拟/字符串】DRM Messages

DRM Encryption is a new kind of encryption. Given an encrypted string (which we’ll call a DRM
message), the decryption process involves three steps: Divide, Rotate and Merge. This process is
described in the following example with the DRM message “EWPGAJRB”:
Divide — First, divide the message in half to “EWPG” and “AJRB”.
Rotate — For each half, calculate its rotation value by summing up the values of each character
(A = 0, B = 1, . . . Z = 25). The rotation value of “EWPG” is 4 + 22 + 15 + 6 = 47. Rotate each
character in “EWPG” 47 positions forward (wrapping from Z to A when necessary) to obtain the
new string “ZRKB”. Following the same process on “AJRB” results in “BKSC”.
Merge — The last step is to combine these new strings (“ZRKB” and “BKSC”) by rotating each character
in the first string by the value of the corresponding character in the second string. For the first
position, rotating ‘Z’ by ‘B’ means moving it forward 1 character, which wraps it around to ‘A’.
Continuing this process for every character results in the final decrypted message, “ABCD”.
Input
The input file contains several test cases, each of them as described below.
The input contains a single DRM message to be decrypted. All characters in the string are uppercase
letters and the string’s length is even and ≤ 15000.
Output
For each test case, display the decrypted DRM message.
Sample Input
EWPGAJRB
UEQBJPJCBUDGBNKCAHXCVERXUCVK
Sample Output
ABCD
ACMECNACONTEST

分析:按照题意模拟,另外使用取模别用减法

#include<iostream>
#include <string>
using namespace std;
typedef long long LL;
string str,part1,part2;
LL sum1, sum2;
void init()
{
  part1="", part2="";
  sum1 = sum2 = 0;
}
void str_divide()
{
  part1 = str.substr(0, str.length() / 2);
  part2 = str.substr(str.length() / 2);
}
void str_rotate()
{
  sum1 = sum2 = 0;
  for (int i = 0; i < part1.length(); i++)
  {
    part1.at(i) -= 'A';
    sum1 += (LL)part1.at(i);
  }
  sum1 %= 26;
  for (int i = 0; i < part2.length(); i++)
  {
    part2.at(i) -= 'A';
    sum2 += (LL)part2.at(i);
  }
  sum2 %= 26;
  for (int i = 0; i < part1.length(); i++)
  {
    part1.at(i) += sum1;
    if (part1.at(i) > 26)
      part1.at(i) -= 26;
  }
  for (int i = 0; i < part2.length(); i++)
  {
    part2.at(i) += sum2;
    if (part2.at(i) > 26)
      part2.at(i) -= 26;
  }
}
void str_merge()
{
  for (int i = 0; i < part1.length(); i++)
  {
    part1.at(i) += part2.at(i);
    part1.at(i) %= 26;
    part1.at(i) += 'A';
  }
}
int main()
{
  while (cin >> str)
  {
    init();
    str_divide();
    str_rotate();
    str_merge();
    cout << part1 << endl;
  }
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

3 × 5 =