A
题意
字符串,相邻两个字符一组,不能有重复的,有几种情况
set是集合,自动不会有重复的 set<类型>变量名 变量名.insert()插入
#include <iostream>
#include <set>
using namespace std;
int main()
{
int t; cin >> t;
while(t --)
{
set<string>st;
int n; cin >> n;
string s; cin >> s;
for(int i = 0; i < n - 1; i ++)
{
string t;
t += s[i];
t += s[i + 1];
st.insert(t);
}
cout << st.size() << endl;
}
return 0;
}
B
题意:
给定an和 bn,重新排序b,使|bi - ai| <= k;
对a和b都进行排序,排序后,按a的原位置输出对应的b就可以
用sort排序a的二维,一维是值,二维是对应的原来的坐标,按照每个子数组中的第一个数递增
排序后ab都是递增,让现在a的坐标对应着b的值,放在此刻a对应的原来的坐标里面,也就是c[v[i].second] = b[i];
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
int main()
{
int t;
cin >> t;
while(t --)
{
int n, k;
cin >> n >> k;
vector<PII>v(n);
vector<int> a(n);
for(int i = 0; i < n; i ++)
cin >> a[i], v[i] = {a[i], i};
sort(v.begin(), v.end());
vector<int>b(n);
for(int i = 0; i < n ; i ++)
cin >> b[i];
sort(b.begin(), b.end());
vector<int>c(n);
for(int i = 0; i < n; i ++)
c[v[i].second] = b[i];
for(int i = 0; i< n; i ++)
cout << c[i] << " ";
cout << endl;
}
return 0;
}
C
题意:
有数组a;通过bi = ai
或者bi = ai - aj (1 <= j <= n)
求新的数组b,要求b全都是奇数或者偶数,不能为负数.
首先从小到大排序;然后判断a[0]的奇偶性,如果为奇数,就要保证后面的数都是奇数或者ai - a[0]是奇数; 如果为偶数,就要保证后面的数都是偶数,或者ai - a[0]是偶数
#include <bits/stdc++.h>
using namespace std;
bool cmp(int x, int y)
{
return x < y;
}
void solved(int n)
{
int a[n];
for(int i = 0; i < n; i ++) cin >> a[i];
sort(a, a + n, cmp);
int k = a[0];
if(k % 2 != 0)
{
for(int i = 1; i < n; i ++)
{
if(a[i] % 2 == 0 && (a[i] - k) % 2 == 0)
{
cout << "no" << endl;
return;
}
}
}
else
{
for(int i = 1; i < n; i ++)
{
if(a[i] % 2 != 0 && (a[i] - k) % 2 != 0)
{
cout << "no" << endl;
return;
}
}
}
cout << "yes" << endl;
}
int main()
{
int t; cin >> t;
while(t --)
{
int n; cin >> n;
solved(n);
}
return 0;
}