题目传送门
1 /*
2 贪心:全排列函数使用,更新最值
3 */
4 #include <cstdio>
5 #include <algorithm>
6 #include <cstring>
7 #include <cmath>
8 #include <string>
9 #include <vector>
10 #include <map>
11 #include <set>
12 #include <iostream>
13 #include <queue>
14 using namespace std;
15
16 typedef long long ll;
17 const int MAXN = 20;
18 const int INF = 0x3f3f3f3f;
19 int b[MAXN];
20 char a[MAXN][MAXN];
21
22 int main(void) //CodeForces 124B Permutations
23 {
24 // freopen ("A.in", "r", stdin);
25
26 int n, k;
27 while (scanf ("%d%d", &n, &k) == 2)
28 {
29 for (int i=1; i<=n; ++i)
30 {
31 scanf ("%s", a[i] + 1);
32 }
33 for (int i=1; i<=k; ++i) b[i] = i;
34
35 ll ans = INF;
36 do
37 {
38 ll mx = 0, mn = INF; ll sum = 0;
39 for (int i=1; i<=n; ++i)
40 {
41 sum = 0;
42 for (int j=1; j<=k; ++j)
43 {
44 sum = sum * 10 + (a[i][b[j]] - '0');
45 }
46 if (sum > mx) mx = sum;
47 if (sum < mn) mn = sum;
48 }
49 ans = min (ans, mx - mn);
50 }while (next_permutation (b+1, b+1+k));
51
52 printf ("%I64d
", ans);
53 }
54
55 return 0;
56 }