Skip to content

Commit 5c0ab38

Browse files
committed
🔨 Refactoring code with string.find() and toupper
1 parent e8727f5 commit 5c0ab38

File tree

1 file changed

+7
-41
lines changed

1 file changed

+7
-41
lines changed

PAT甲级(Advanced Level)/1084. Broken Keyboard (20).cpp

+7-41
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,14 @@ Sample Output:
1818
7TI
1919

2020
#include <iostream>
21+
#include <cctype>
2122
using namespace std;
2223
int main() {
23-
string a;
24-
cin >> a;
25-
int lena = a.length();
26-
string b;
27-
cin >> b;
28-
int lenb = b.length();
29-
char c[100];
30-
int i = 0, j = 0, t = 0;
31-
32-
while (j <= lenb - 1) {
33-
if (b[j] != a[i]) {
34-
c[t++] = a[i];
35-
i++;
36-
} else {
37-
i++;
38-
j++;
39-
}
40-
}
41-
42-
if (i != lena) {
43-
for (int k = i; k <= lena - 1; k++) {
44-
c[t++] = a[k];
45-
}
46-
}
47-
48-
for (int k = 0; k < t; k++) { if (c[k] >= 'a' && c[k] <= 'z')
49-
c[k] = c[k] - 32;
50-
}
51-
52-
for (int k = 0; k < t; k++) {
53-
for (int m = 0; m < k; m++) {
54-
if (c[m] == c[k]) {
55-
c[k] = '#';
56-
}
57-
}
58-
}
59-
60-
for (int k = 0; k < t; k++) {
61-
if (c[k] != '#')
62-
cout << c[k];
63-
}
24+
string s1, s2, ans;
25+
cin >> s1 >> s2;
26+
for (int i = 0; i < s1.length(); i++)
27+
if (s2.find(s1[i]) == string::npos && ans.find(toupper(s1[i])) == string::npos)
28+
ans += toupper(s1[i]);
29+
cout << ans;
6430
return 0;
6531
}

0 commit comments

Comments
 (0)