PTA-1029-旧键盘

题目链接

https://pintia.cn/problem-sets/994805260223102976/problems/994805292322111488

数据范围:

字符串长度小于80,且两个串非空

样例:

Input:

1
2
7_This_is_a_test
_hs_s_a_es

Output:

1
7TI

题目做法

我们用$string ans$,$ans$用来保存当前的“坏键” 我们考虑从$s1$的字串中,扫一遍,如果在字串$s2$没有对应的字符,并且它的大写字母也不在$ans$中时,我们把该字母添加到$ans$中。

这里要用到两个函数,分别是$string.find()$和$toupper()$。

  1. $string.find()$函数

    https://en.cppreference.com/w/cpp/string/basic_string/find

    用来查找给定字符序列的第一个子字符串,搜索始于$pos$,返回字串的内容或者$npos$的第一个字符的位置。【如果找不到的话】。

    我们采用$npos$特殊值来表示找不到。该值一般表示该类型的最大值。

    用法详见代码。

  2. $toupper()$函数

    将一个字符转换成大写。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Copyright(c)
* Author : tiketiskte
*/
#include <bits/stdc++.h>
#define IOS {ios::sync_with_stdio(false);cin.tie(0);}
#define INF 0x3f3f3f3f

using namespace std;

int main()
{
IOS
string s1, s2;
cin >> s1 >> s2;
string ans;
int l1 = s1.length(), l2 = s2.length();
for(int i = 0; i <= l1; i++) {
if(s2.find(s1[i]) == string::npos && ans.find(toupper(s1[i])) == string::npos) {
ans += toupper(s1[i]);
}
}
cout << ans << endl;
//system("pause");
return 0;
}

总结点

无(占坑)

参考资料

无(占坑)