acwing周赛36
1.处理字符串(模拟)
【题目链接】4215. 处理字符串 - AcWing题库
大小写转换:
首先大小写相差32,转换的话自己写函数也是可以写出来的
- 大写转小写
low_ch = tolower(high_ch);
- 小写转大写
high_ch = toupper(low_ch);
string find()函数:
- 找到(存在)返回它的下标(起始位置)------> 存在:
if(str.find(c) != -1)
- 不存在,返回
-1
【代码实现】
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
string s, ans = "";
cin >> s;
for (int i = 0; i < s.size(); i ++ )
if(s[i] >= 'A' && s[i] <= 'Z')
s[i] = s[i] + 32;// 大写转小写
for (int i = 0; i < s.size(); i ++ )
{
if(s[i] == 'a' || s[i] == 'o' || s[i] == 'y' || s[i] == 'e' || s[i] == 'u' || s[i] == 'i') continue;
else
{
cout << '.';
cout << s[i];
}
}
return 0;
}
【代码2】
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
string s;
cin >> s;
string cs = "aoyeui";
for(auto &c : s)
{
c = tolower(c);// 转小写
if(cs.find(c) != -1) continue;// s中存在元音字母则跳过
else cout << '.' << c;
}
}
2. 图中的环(基环树、并查集)
【题目链接】4216. 图中的环 - AcWing题库
基环树(基于环的树):一个有且仅有一个环并且连通的图。(可以看做是一个环上接了一些树)
树:n = m - 1
基环树的必要条件:
- 只有一个环:
m = n
- 连通:并查集
【补充】
p —> q
q是p的必要条件(若p真则q真)
p是q的充分条件(若q真,p可真可假)
反过来,若一个图连通,那么一定可以求它的一个生成树(n = m - 1),又因为 m = n说明它多了一条边,即一定有一个环!即它是充分的!
【代码实现】
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int p[N];
int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
int n, m;
cin >> n >> m;
if(n != m) puts("NO");// 说明改图不是含一个环的图
else
{
for (int i = 1; i <= n; i ++ ) p[i] = i;// 初始化并查集
int cnt = n;
while (n -- )
{
int a, b;
cin >> a >> b;
if(find(a) != find(b))
{
p[find(a)] = find(b);// 合并
cnt --;
}
}
if(cnt == 1) puts("YES");// 1说明该含环图是联通的
else puts("NO");
}
}
3.机器人移动(二分、前缀和、双指针)
【题目链接】4217. 机器人移动 - AcWing题库
【待补】
例题来源:acwing周赛