A - chukodai
题意:给定一个由小写英文字母组成的字符串S。交换从S的开头开始的第a个和第b个字符,并打印结果字符串。
思路: 直接swap()交换即可
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100010, INF = 0x3f3f3f3f, mod = 998244353;
string s;
int a, b;
int main()
{
cin >> s;
cin >> a >> b;
swap(s[a - 1], s[b - 1]);
cout << s;
return 0;
}
B - Who is missing?
题意: 假设被 Takahashi 移除的卡片上写的整数为X。根据题意可知,总共有4N张卡片,且每个整数i都有4张卡片。所以卡片上整数的总和为:总和 = 1 + 2 + 3 + … + N = N * (N + 1) / 2,由于被移除的卡片上的整数X只有一张,所以通过计算剩余卡片上整数的总和,然后用总和减去剩余卡片上整数的总和,就可以找到被移除的卡片上的整数X。
思路: 使用map函数统计每个数字出现的次数,将出现次数不等于4的那个数输出。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100010, INF = 0x3f3f3f3f, mod = 998244353;
int n, x;
map<int, int> mp;
int main()
{
cin >> n;
for(int i = 1; i <= 4 * n - 1; i ++)
{
cin >> x;
mp[x] ++;
}
for(int i = 1; i <= n; i ++)
{
if(mp[i] != 4)
{
cout << i;
break;
}
}
return 0;
}
C - Route Map
题意: 有一条由AtCoder Railway经营的线路上有N个车站。从起始站开始,第i个车站(1≤i≤N)被命名为Si。
普通列车在所有车站停靠,而快速列车可能不会停靠。具体来说,快速列车只在M(M≤N)个车站停靠,且第j个停靠站(1≤j≤M)是命名为Tj的车站。在这里,确保T1=S1和TM=SN,也就是说,快速列车在起始和终点车站都停靠。
对于每个N个车站,确定快速列车是否停靠在该车站。
思路: 和B题思路类似,用map记录特快列车是否在各站停靠,然后再遍历一遍S数组输出结果
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100010, INF = 0x3f3f3f3f, mod = 998244353;
int n,m;
string s[N];
string t;
map<string,bool> mp;
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i ++)
{
cin >> s[i];
}
for(int i = 0; i < m; i ++)
{
cin >> t;
mp[t] = true;
}
for(int i = 0; i < n; i ++)
{
if(mp[s[i]]) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}