1. 密码学
小明学会了一种加密方式。他定义suc(x)为x在字母表中的后继,例如a的后继为b,b的后继为c… (即按字母表的顺序后一个)。特别的,z的后继为a。对于一个原字符串S,将其中每个字母x都替换成其三重后继,即suc(suc(suc(x)))的字母,即完成了加密。
例如,abc加密后变成def (suc(suc(suc(a)))=d suc(suc(suc(b)))=e suc(suc(suc(c)))=f)
现在小明知道一个加密后的字符串S',想请你找出他的原串S
输入描述:
第一行一个正整数N,表示加密后的字符串长度
接下来一行是长度为N的字符串S',含义如上。保证仅包含小写英文字母。
对于所有数据:1≤N≤50000
输出描述:
一行,一个长度为N的字符串S,表示答案。
样例输入:
3
def
样例输出:
abc
思路:打卡题
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;
int main(){
int n; cin >> n;
string s; cin >> s;
string temp;
for (int i = 0; i < n; i++) {
int p = (s[i] - 'a' + 23) % 26;
temp += ('a' + p);
}
cout << temp;
return 0;
}
2. K排序
在算法中,有各种各样的排序算法,例如归并排序,冒泡排序,快速排序等等。本题中,我们会使用一种新的排序算法:K排序。K排序算法描述如下:首先,算法需要按照某种规则选择该数列上至多K个位置,将其对应的数抽出来,其他的数都往左对齐,之后这K个数排好序之后依次放在原数列末尾。以上过程算作一次操作。例如,对于数列[1,3,5,4,2],当K=2时可以选择数字5和4,之后数列变成[1,3,2,4,5]。你的任务是:对于给定的数列,你需要计算出最少需要多少次上述操作,使得整个数列从小到大排好序?
输入描述:
第一行一个正整数T,表示有T组数据。
对于每一组数据,第一行输入两个正整数n,k;第二行输入n个数a1,a2,....,an。该序列是一个1~n的排列。
对于所有数据:1≤k≤n≤105,1≤ai≤n, ai≠aj,1≤T≤5
输出描述:
对于每一组数据,输出一行一个整数,表示答案。
样例输入:
2
5 1
1 2 3 4 5
5 2
1 3 5 4 2
样例输出:
0
2
思路:前方有序不需要调整,从第一个无序开始都需要调整
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;
int main(){
int T; cin >> T;
while (T--) {
int n, k; cin >> n >> k;
int res = 0;
for (int i = 1; i <= n; i++) {
int temp = 0;
cin >> temp;
if (temp != i && res == 0) {
res = i;
}
}
if (res == 0) cout << 0 << endl;
else {
cout << (n - res) / k + 1 << endl;
}
}
}