「PAT乙级真题解析」Basic Level 1093 字符串A+B (问题分析+完整步骤+伪代码描述+提交通过代码)
题设要求找出给定的两个字符串中字符的并集,并且按照在字符串中出现的顺序输出并集。 这意味着我们需要依次输出字符串中的各个字符, 如果这个字符已经被输出过了, 就不再输出。 C语言中没有原生集合类型, 可以使用数组代替。
#算法#pat考试#数据结构#c语言#需求分析
Table of Contents
乙级的题目训练主要用来熟悉编程语言的语法和形成良好的编码习惯和编码规范。从小白开始逐步掌握用编程解决问题。
PAT (Basic Level) Practice 1093 字符串A+B
问题分析
题设要求找出给定的两个字符串中字符的并集,并且按照在字符串中出现的顺序输出并集。 这意味着我们需要依次输出字符串中的各个字符, 如果这个字符已经被输出过了, 就不再输出。 C语言中没有原生集合类型, 可以使用数组代替。
完整描述步骤
- 获取输入: 字符串A, 字符串B
- 初始化记录器:
- 字符是否被输出过的标记位 = {0}
- 对于字符串A中的每一个字符:
- 如果这个字符没有输出过(在字符输出标记位中为0或False):
- 输出这个字符
- 字符是否被输出过的标记位[当前字符] = True
- 如果这个字符没有输出过(在字符输出标记位中为0或False):
- 对于字符串B中的每一个字符:
- 如果这个字符没有输出过(在字符输出标记位中为0或False):
- 输出这个字符
- 字符是否被输出过的标记位[当前字符] = True
- 如果这个字符没有输出过(在字符输出标记位中为0或False):
伪代码描述
- get input: string A and string B
- init recorder:
- printed = {False};
- for char in A:
- if not printed[char]:
- print(char);
- printed[char] = True;
- if not printed[char]:
- for char in B:
- if not printed[char]:
- print(char);
- printed[char] = True;
- if not printed[char]:
完整提交代码
/*
# 问题分析
题设要求找出给定的两个字符串中字符的并集,并且按照在字符串中出现的顺序输出并集。
这意味着我们需要依次输出字符串中的各个字符, 如果这个字符已经被输出过了, 就不再输出。
C语言中没有原生集合类型, 可以使用数组代替。
# 完整描述步骤
1. 获取输入: 字符串A, 字符串B
2. 初始化记录器:
- 字符是否被输出过的标记位 = {0}
3. 对于字符串A中的每一个字符:
- 如果这个字符没有输出过(在字符输出标记位中为0或False):
- 输出这个字符
- 字符是否被输出过的标记位[当前字符] = True
4. 对于字符串B中的每一个字符:
- 如果这个字符没有输出过(在字符输出标记位中为0或False):
- 输出这个字符
- 字符是否被输出过的标记位[当前字符] = True
# 伪代码描述
1. get input: string A and string B
2. init recorder:
- printed = {False};
3. for char in A:
- if not printed[char]:
- print(char);
- printed[char] = True;
4. for char in B:
- if not printed[char]:
- print(char);
- printed[char] = True;
*/
# include<stdio.h>
# include<string.h>
char A[1000010];
char B[1000010];
int main(){
fgets(A, 1000010, stdin);
A[strlen(A) - 1] = 0;
fgets(B, 1000010, stdin);
B[strlen(B) - 1] = 0;
int printed[256] = {0};
for (int i = 0; A[i]; i++){
if (printed[A[i]] == 0){
printf("%c", A[i]);
printed[A[i]] = 1;
}
}
for (int i = 0; B[i]; i++){
if (printed[B[i]] == 0){
printf("%c", B[i]);
printed[B[i]] = 1;
}
}
return 0;
}