天天看点

B1093 字符串A+B(python)

1093 字符串A+B (20分)

给定两个字符串 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

AC

分析:思路如下:

  1. 首先,将接收到的a和b字符串组合起来求集合去重
  2. 利用字典,构造所有a和b中的字符信息标志,将所有值都置为0
  3. 定义函数single,当当前字符在字典中的值为0时(即没有输出过),输出该字符,并将其在字典中的值置为1
  4. 调用single,a字符串在前,b字符串在后~
a = input()
b = input()
d = set(a + b)
flag = {}
for item in d:
    flag[item] = 0
def single(ls):
    for s in ls:
        if flag[s] == 0:
            print(s, end='')
            flag[s] = 1
single(a)
single(b)
           

继续阅读