题目链接:1093 字符串A+B (20 point(s))
题目描述
给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集。要求先输出 A,再输出 B,但重复的字符必须被剔除。
输入格式
输入在两行中分别给出 A 和 B,均为长度不超过 106的、由可见 ASCII 字符 (即码值为32~126)和空格组成的、由回车标识结束的非空字符串。
输出格式
在一行中输出题面要求的 A 和 B 的和。
输入样例
This is a sample test
to show you_How it works
输出样例
This ampletowyu_Hrk
Java代码
/**********************************************************************************
Submit Time Status Score Problem Compiler Run Time User
7/31/2019, 21:04:26 Accepted 20 1093 Java (openjdk) 225 ms wowpH
**********************************************************************************/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
private static void output(char[] arr, boolean[] ascii) {
for (int i = 0; i < arr.length; ++i) {
if (false == ascii[arr[i]]) {
System.out.print(arr[i]);
ascii[arr[i]] = true;
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] a = br.readLine().toCharArray();
char[] b = br.readLine().toCharArray();
boolean[] ascii = new boolean[127];
output(a, ascii);
output(b, ascii);
}
}
Java提交结果
C代码
/*********************************************************************************
Submit Time Status Score Problem Compiler Run Time User
7/31/2019, 20:35:10 Accepted 20 1093 C (gcc) 17 ms wowpH
*********************************************************************************/
#include<stdio.h>
#include<memory.h>
int main() {
char ascii[127], ch; // ascii[]保存字符是否已输出过
memset(ascii, 0, sizeof(ascii)); // 0表示未输出过
ascii[10] = 1; // 不输出换行,'\n'的ASCII值为10
while ((ch = getchar()) != EOF) { // 接收字符,直到结束
if (0 == ascii[ch]) { // 字符ch未输出过
printf("%c", ch); // 输出字符ch
ascii[ch] = 1; // 设ch为已输出过
}
}
return 0;
}