Skip to content

02039: 反反复复

http://cs101.openjudge.cn/practice/02039/

Mo和Larry发明了一种信息加密方法。他们首先决定好列数,然后将信息(只包含字母)从上往下依次填入各列,并在末尾补充一些随机字母使其成为一个完整的字母矩阵。例如,若信息是“There's no place like home on a snowy night”并且有5列,Mo会写成:

t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x

注意Mo只会填入字母,且全部是小写形式。在这个例子中,Mo用字母“x”填充了信息使之成为一个完整的矩阵,当然他使用任何字母都是可以的。

Mo根据这个矩阵重写信息:首先从左到右写下第一行,然后从右到左写下第二行,再从左到右写下第三行……以此左右交替地从上到下写下各行字母,形成新的字符串。这样,例子中的信息就被加密为:toioynnkpheleaigshareconhtomesnlewx。

你的工作是帮助Larry从加密后的信息中还原出原始信息(包括填充的字母)。

输入

第一行包含一个整数(范围2到20),表示使用的列数。 第二行是一个长度不超过200的字符串。

输出

一行,即原始信息。

样例输入

5
toioynnkpheleaigshareconhtomesnlewx

样例输出

theresnoplacelikehomeonasnowynightx

来源

East Central North America 2004

python
# 23n2300011072(X)
cols = int(input())
encrypted = input()
# 计算行数
rows = len(encrypted) // cols
# 创建矩阵
matrix = [['' for _ in range(cols)] for _ in range(rows)]
# 填充矩阵
index = 0
for row in range(rows):
    if row % 2 == 0:  # 从左到右填充
        for col in range(cols):
            matrix[row][col] = encrypted[index]
            index += 1
    else:  # 从右到左填充
        for col in range(cols - 1, -1, -1):
            matrix[row][col] = encrypted[index]
            index += 1
# 从矩阵中提取原始信息
original = ''
for col in range(cols):
    for row in range(rows):
        original += matrix[row][col]
print(original)