Codeforces Round #751 (Div. 2)
A题
传送门
题意:把字符串最小的那个字符输出到最前面,剩下的字符不变全写在后面

code:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
using namespace std;
int T;
string s1;
string s2;
bool st[2000];
int main(){
cin >> T;
while(T--){
cin >> s1;
s2 = s1;
memset(st,0,sizeof(st));
sort (s1.begin(),s1.end());
for (int i = 0; i < s1.length(); i ++){
if (s1[0] == s2[i]){
st[i] = true;
break;
}
}
cout << s1[0] << " ";
for (int i = 0; i < s1.length(); i ++)
if(!st[i])
cout << s2[i];
cout << endl;
// cin >> s1;
// s2 = s1;
// sort(s1.begin(), s1.end());
// if(s1.size() == 2){
// cout << s1[0] << ' ' << s1[1] << endl;
// continue;
// }
//
// for (int i = 0; i < s2.size(); i++){
// if(s1[0] == s2[i]){
// s2 = s2.substr(0, i) + s2.substr(i + 1);
// break;
// }
// }
// cout << s2 << endl; // cout << s1[0] << ' ' << s2 << endl;
}
}
B题
传送门
题意:给定一个数组,这个数组的每个数字重复几次,下次就更新为几,有q次询问,求第k次操作的数组中下标为x的数值(下标从1开始)
题解:我们创建一个二维数组a[N][N],数组的行代表有N次循环,列代表吗,每次循环的各个数值,首先让a[0][N]是初试数组,后续数组就可以从1开始
注意:我们可以任意列举数值,不难发现,数组长度为n,进行的循环操作最多不会超过n次,就会让数组的数值衡定下来,所以第k次操作如果k>n,我们完全可以让k=n
code:
/*
1 2 -> 1 1
1 2 2 -> 1 2 2 , 1 2 3 -> 1 1 1, 3 3 3
1 2 4 4 -> 1 1 2 2 , 2 2 2 2 , 4 4 4 4 ; 1 1 1 2 -> 3 3 3 1
1 3 2 2 2 -> 1 1 3 3 3 ,2 2 3 3 3 ; 1 2 2 4 4 -> 1 2 2 2 2 ,1 4 4 4 4
*/
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int a[2010][2010];
int ans[2010];
int st[2021];
int main(){
int t;
cin >> t;
while (t --){
int n;
cin >> n;
//输入初始数组
for (int i = 1; i <= n; i ++)
cin >> a[0][i];
//进行n次循环操作
for (int i = 1; i <= n; i ++){
for (int j = 1; j <= n; j ++)
ans[j] = 0;
// 统计出现次数
for (int j = 1; j <= n; j ++)
ans[a[i-1][j]] ++;
// 更新第i次循环的数值
for (int j = 1; j <= n; j ++)
a[i][j] = ans[a[i-1][j]];
}
int q;
cin >> q;
while (q --){
int k,x;
cin >> x >> k;
if (k > n) k = n;
cout << a[k][x] << endl;
}
}
return 0;
}

浙公网安备 33010602011771号