题解:B4303 [蓝桥杯青少年组省赛 2024] 字母移位
题目简述
给定长度为
特别地,a 向左移动变为 z,z 向右移动变为 a。
主要思路
由于共有
对于
但注意:z 处于 ASCII 码很后面的位置,所以当 z 加上一个数后,就有可能炸掉 char,那么就可以先用一个 int 变量来存储
AC Code
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double db;
const int N = 1e5 + 10;
const int INT_INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
// ----------------------------
// ----------------------------
ll a[N], pre[N];
// ----------------------------
int main() {
int n; cin >> n;
string s; cin >> s;
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] %= 26;
if (i & 1) a[i] = -a[i];
pre[i] = pre[i - 1] + a[i];
pre[i] %= 26;
}
// ----------------------------
for (int i = 0; i < n; i++) {
int k = s[i] + (pre[n] - pre[i]) % 26;
if (k > 'z') k -= 26;
if (k < 'a') k += 26;
s[i] = k;
}
// ----------------------------
cout << s;
return 0;
}