Progressive Scramble

题目描述

You are a member of a naive spy agency. For secure communication,members of the agency use a very simple encryption algorithm – which changes each symbol in the message ‘progressively’, i.e., based on the symbols preceding it. The allowed symbols are space and the 26 lowercase English letters. For encryption purposes we assign them the values 0 (for space) and 1 through 26 (for a–z). We’ll let v(s) represent the numeric value of symbol s.
Consider a message with symbols s1, s2, . . . , sn. The encryption algorithm starts by converting the first symbol s1 into its associated value u1 = v(s1). Then for each subsequent symbol si in the message, the computed value is ui = v(si) + ui−1 — the sum of its associated value and the computed value for the previous symbol. (Note that when there is a space in the input
message, the previous scrambled letter is repeated.) This process continues until all the ui are computed.
At this point, the message is a sequence of numeric values. We need to convert it back to symbols to print it out. We do this by taking the value uimodulo 27 (since there are 27 valid symbols), and replacing that value with its corresponding symbol. For example, if ui = 32, then 32 mod 27 = 5, which is the symbol ‘e’ (since v(e) = 5).
Let’s look at an example. Suppose we want to encrypt the string “my pie”. 
1. First, convert each symbol si into v(si): [13, 25, 0, 16, 9, 5].
2. Next, compute each ui: [13, 38, 38, 54, 63, 68].
3. Then, use modulus on the ui: [13, 11, 11, 0, 9, 14].
4. Finally, convert these back to symbols: “mkk in”.
Create a program that takes text and encrypts it using this algorithm, and also decrypts text that has been encrypted with this algorithm.

 

输入

The input to your program consists of a single integer 1 ≤ n ≤ 100 on its own line. This number is followed by n lines, each containing the letter ‘e’ or ‘d’, a single space, and then a message made up of lowercase letters (a–z) and spaces, continuing to the end of the line. Each message is between 1 and 80 characters long. The letters ‘d’ and ‘e’ indicate that your program decrypts or encrypts the subsequent string, respectively.

 

输出

Output the result of encrypting or decrypting each message from the input on its own separate line. Note that differences in whitespace are significant in this problem. Therefore your output must match the correct output character-for-character, including spaces.

 

样例输入

7
e testing multiple letters rrrrrrrrrrrrr
e this particularly long  sentence can test encryption
d tajbbrsjcloiuvmywwhwjqqqinauzmpuuxyllejbvv nqhfvoxlz
e my pie
d mkk in
e the quick brown fox jumps over the lazy dog
d taffwqzbmmofuqddjyvvezlatthchzzs eeqrqoosgn

样例输出

tyqjsfmmzteygwhmmycwpulddvmdvmdvmdvmdv
tajbbrsjcloiuvmywwhwjqqqinauzmpuuxyllejbvv nqhfvoxlz
this particularly long  sentence can test encryption
mkk in
my pie
taffwqzbmmofuqddjyvvezlatthchzzs eeqrqoosgn
the quick brown fox jumps over the lazy dog

题意:

给你一个字符串,空格 值 为0 ,字母的值是1-26,然后给你加密公式,s[i] = sum[i] % 27,sum[i] 表示 前i个数值得总和,s[i]就是加密后的结果。解密的话就倒推,由于第一个字符肯定是不变的,所以我们以他的值为基础,s[i] = (now - sum)%27,sum是求得的前i-1个数加密前的总和,now是当前加密后字符的价值不断加27,加到刚好比sum大的那个值,这样两者一减就是加密前那个字符的价值了,一开始我是用两个字符串去存,一个是存e ,d指令,一个是存s串,这样就导致我一直在WA,后来发现一个问题,如果s串是以空格开始,那么这个空格是读不进去的,所以我就改成了就用一个s串去存,然后把前两位用作指令的判断。要是训练赛时能机灵点就好了,还是太菜了,TAT

AC代码

#include<iostream>
#include<cstdio>     //EOF,NULL
#include<cstring>    //memset
#include<cstdlib>    //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<cmath>           //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm>  //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<iomanip>             //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<functional>
#include<map>
#include<set>
#include<limits.h>     //INT_MAX
#include<cmath> // bitset<?> n
using namespace std;
#define pb push_back
#define mp make_pair
#define rep(i,a,n) for(int i=a;i<n;++i)
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define sca(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
typedef pair<int,int> P;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const int MAXN = 105;
const int maxn = 10010;
  
char op[5];
int ans[666];
string s;
int main()
{
  int n;
  int t;
  sca(t);
  getchar();
  while(t--)
  {
    getline(cin,s);
    if(s[2] == ' ')
    {
      ans[2] = 0;
    }
    else
    {
      ans[2] = s[2] - 'a' + 1;
    }
    if(s[0] == 'e')
    {
      rep(i,3,s.length())
      {
        int now;
        if(s[i] == ' ')
        {
          now = 0;
        }
        else
        {
          now = s[i] - 'a' + 1;
        }
        ans[i] = ans[i-1] + now;
      }
      rep(i,2,s.length())
      {
        if(ans[i]%27 == 0)
          printf(" ");
        else
          printf("%c",ans[i]%27 - 1 + 'a' );
      }
      printf("\n");
    }
    else
    {
      int sum = ans[2];
      rep(i,3,s.length())
      {
        int now;
        if(s[i] == ' ')
        {
          now = 0;
        }
        else
        {
          now = s[i] - 'a' + 1;
        }
        while(now < sum)
          {
            now += 27;
          }
        ans[i] = now - sum;
        sum += ans[i];
      }
      rep(i,2,s.length())
      {
        if(ans[i]%27 == 0)
          printf(" ");
        else
          printf("%c",ans[i]%27 - 1 + 'a' );
      }
      printf("\n");
    }
  }
  return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值