300651: CF124B. Permutations

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Permutations

题意翻译

给你 $n$ 个 $k$ 位数,您需要重新排列整数中的各位数字,使得最大数和最小数的差最小。所有整数的重排规则应该都相同。 ### 输入格式: 第一行包含两个整数 $n$ 和 $k$ $( 1 \leq n,k \leq 8 )$,分别代表数字的个数和位数,接下来 $n$ 行,每一行包含一个 $k$ 位正整数,调换顺序前后的数均允许出现前导零。 ### 输出格式: 仅一行一个数字,按照相同规则将所有整数重新排列后,最大数和最小数的差的最小值。 ### 说明/提示: 在样例$1$中,我们将数字重新排列为$(3,1,4,2)$,则第 $2$ 个和第 $4$ 个数字将分别等于 $5237$ 和 $2537$ (它们是用该顺序调换后最大和最小的数字)。 在第二个示例中,我们交换第二个数字和第一个数字,我们会得到整数 $100$ 、$99$ 和 $102$ 。

题目描述

You are given $ n $ $ k $ -digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers.

输入输出格式

输入格式


The first line contains integers $ n $ and $ k $ — the number and digit capacity of numbers correspondingly ( $ 1<=n,k<=8 $ ). Next $ n $ lines contain $ k $ -digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits.

输出格式


Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule.

输入输出样例

输入样例 #1

6 4
5237
2753
7523
5723
5327
2537

输出样例 #1

2700

输入样例 #2

3 3
010
909
012

输出样例 #2

3

输入样例 #3

7 5
50808
36603
37198
44911
29994
42543
50156

输出样例 #3

20522

说明

In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits). In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.

Input

题意翻译

给你 $n$ 个 $k$ 位数,您需要重新排列整数中的各位数字,使得最大数和最小数的差最小。所有整数的重排规则应该都相同。 ### 输入格式: 第一行包含两个整数 $n$ 和 $k$ $( 1 \leq n,k \leq 8 )$,分别代表数字的个数和位数,接下来 $n$ 行,每一行包含一个 $k$ 位正整数,调换顺序前后的数均允许出现前导零。 ### 输出格式: 仅一行一个数字,按照相同规则将所有整数重新排列后,最大数和最小数的差的最小值。 ### 说明/提示: 在样例$1$中,我们将数字重新排列为$(3,1,4,2)$,则第 $2$ 个和第 $4$ 个数字将分别等于 $5237$ 和 $2537$ (它们是用该顺序调换后最大和最小的数字)。 在第二个示例中,我们交换第二个数字和第一个数字,我们会得到整数 $100$ 、$99$ 和 $102$ 。

加入题单

算法标签: