这不是签到题吗?
主要思路
由于每 组为一个周期,不难想到用取模来计算偏移量。
认真读题,发现偏移量可能为负数。
场上同学没想到负数的情况,挂分了。
如果它是大写字母 A 到 Z,则计算它在字母表中的位置 pos = s[i] - 'A'。
加上 后对 取模,得到新的位置 (pos + k) % 26,再转换回字母。
AC code
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define TRACE 1
#define tcout TRACE && cout
#define fst ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define int long long
const int P = 998244353;
const int Base = 322225477;
const int INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e6 + 10, M = 2e3 + 10;
int n ;
int k ;
string s ;
signed main()
{
fst ;
cin >> n >> k >> s ;
k = (k % 26 + 26) % 26 ;
for (int i = 0 ; i < n ; i ++)
{
if (s[i] >= 'A' && s[i] <= 'Z')
{
int p = (s[i] - 'A' + k) % 26 ;
s[i] = char('A' + p) ;
}
}
cout << s ;
return 0;
}