题目:B4303 [蓝桥杯青少年组省赛 2024] 字母移位
题目描述
字母移位表示将字母按照字母表的顺序进行移动。
例如, b \texttt{b} b 向右移动一位是 c \texttt{c} c, f \texttt{f} f 向左移动两位是 d \texttt{d} d。
特别地, a \texttt{a} a 向左移动一位是 z \texttt{z} z, z \texttt{z} z 向右移动一位是 a \texttt{a} a。
给定一个仅包含小写字母且长度为 n n n 的字符串 s s s,以及 n n n 个正整数 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an,接下来对字符串 s s s 按如下规律操作:
- 将第 1 1 1 位字符向左移动 a 1 a_1 a1 位;
- 再将第 1 1 1、 2 2 2 位字符都向右移动 a 2 a_2 a2 位;
- 再将第 1 1 1、 2 2 2、 3 3 3 位字符都向左移动 a 3 a_3 a3 位;
- 再将第 1 1 1、 2 2 2、 3 3 3、 4 4 4 位字符都向右移动 a 4 a_4 a4 位;
以此类推,直到将 s s s 的第 1 1 1 到第 n n n 位字符都(按规律向左或向右)移动 a n a_n an 位。
最后,将操作完成后的字符串 s s s 输出。
例如, n = 5 n=5 n=5,字符串 s = abcde s=\texttt{abcde} s=abcde, 5 5 5 个正整数为 1 , 3 , 5 , 7 , 9 1, 3, 5, 7, 9 1,3,5,7,9:
- 将 abcde \texttt{abcde} abcde 的第 1 1 1 位字符 a \texttt{a} a 向左移动 1 1 1 位, s s s 变为 zbcde \texttt{zbcde} zbcde;
- 再将 zbcde \texttt{zbcde} zbcde 的前 2 2 2 位字符 zb \texttt{zb} zb 向右移动 3 3 3 位, s s s 变为 cecde \texttt{cecde} cecde;
- 再将 cecde \texttt{cecde} cecde 的前 3 3 3 位字符 cec \texttt{cec} cec 向左移动 5 5 5 位, s s s 变为 xzxde \texttt{xzxde} xzxde;
- 再将 xzxde \texttt{xzxde} xzxde 的前 4 4 4 位字符 xzxd \texttt{xzxd} xzxd 向右移动 7 7 7 位, s s s 变为 egeke \texttt{egeke} egeke;
- 再将 egeke \texttt{egeke} egeke 的前 5 5 5 位字符 egeke \texttt{egeke} egeke 向左移动 9 9 9 位, s s s 变为 vxvbv \texttt{vxvbv} vxvbv。
最后,将操作完成后的字符串 vxvbv \texttt{vxvbv} vxvbv 输出。
输入格式
第一行,输入一个整数 n n n( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1≤n≤105)。
第二行,输入一个仅包含小写字母且长度为 n n n 的字符串 s s s。
第三行,输入 n n n 个整数 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an( 1 ≤ a ≤ 1 0 9 1 \leq a \leq 10^9 1≤a≤109),整数之间以一个空格隔开。
输出格式
输出一个字符串,表示操作完成后的字符串 s s s。
输入输出样例 #1
输入 #1
5
abcde
1 3 5 7 9
输出 #1
vxvbv
代码(Unaccepted,20分)
#include<iostream>
using namespace std;
const int Maxn = 1e5 + 10;
int n;
string s;
int a[Maxn];
string letter = "abcdefghijklmnopqrstuvwxyz";
int findd(char y){
for(int i = 0; i < letter.size(); i ++){
if(letter[i] == y){
return i;
}
}
}
int main(){
cin >> n;
cin >> s;
for(int i = 1; i <= n; i ++){
cin >> a[i];
}
for(int i = 1; i <= n; i ++){
int k = a[i]; // 移动的位数
for(int j = 0; j < i; j ++){
int indexY = findd(s[j]);
int indexN = 0;
if(i % 2 == 0){ // 偶数右移
indexN = (indexY + k) % 26;
}
else{ // 奇数左移
indexN = (indexY - k + 26) % 26;
}
s[j] = letter[indexN];
}
}
for(int i = 0; i < s.size(); i ++){
cout << s[i];
}
return 0;
}
代码(AC,100分)
#include<iostream>
using namespace std;
typedef long long LL;
const int Maxn = 1e5 + 10;
int n;
string s;
int a[Maxn];
LL num[Maxn];
int main(){
cin >> n;
cin >> s;
for(int i = 1; i <= n; i ++){
cin >> a[i];
if(i & 1){ // i为奇数
a[i] = -a[i];
}
}
for(int i = n; i >= 1; i --){
num[i] = num[i + 1] + a[i];
}
for(int i = 1, j = 0; i <= n; i ++, j ++){
LL k = num[i];
s[j] = (((s[j] - 'a') + k) % 26 + 26) % 26 + 'a';
}
for(int i = 0; i < s.size(); i ++){
cout << s[i];
}
return 0;
}
结果
参考:
AC代码参考洛谷B4303 [蓝桥杯青少年组省赛 2024] 字母移位题解:
感谢大佬的分享,侵删!