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.

Input

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

Output

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.

Sample test(s)
input
6 4
5237
2753
7523
5723
5327
2537
output
2700
input
3 3
010
909
012
output
3
input
7 5
50808
36603
37198
44911
29994
42543
50156
output
20522
Note

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.

题意:给你n个字符串,每个字符串的长度都为k,然后对每个字符串中的字母顺序进行调换,当一个字符串调换顺序时,其他字符串都以相同的规律调换,问你调换过程中最大的数和最小的数的差的最小值是多少。

思路:只要把所有情况搜出来就行了,刚开始还想找规律。。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
char s[100][100],str[100];
int vis[100],k,cha,b[100],n;
#define inf 99999999
void dfs(int wei)
{
int i,j,minx=inf,maxx=-inf,num;
if(wei==k+1){
num=0;
for(i=1;i<=n;i++){
num=0;
for(j=1;j<=k;j++){
num=num*10+s[i][b[j]]-'0';
}
maxx=max(maxx,num);
minx=min(minx,num);
}
cha=min(cha,maxx-minx);//return;
}
else{
for(i=1;i<=k;i++){
if(!vis[i]){
vis[i]=1;
b[wei]=i;
dfs(wei+1);
vis[i]=0;
}
}
}
} int main()
{
int m,i,j,t,num;
char c;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%s",s[i]+1);
}
cha=inf;
if(n==1){
printf("0\n");continue;
}
memset(vis,0,sizeof(vis));
dfs(1);
printf("%d\n",cha);
}
return 0;
}

Codeforces Beta Round #92 (Div. 2 Only) B. Permutations的更多相关文章

  1. Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力

    A. Prime Permutation 题目连接: http://www.codeforces.com/contest/123/problem/A Description You are given ...

  2. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  3. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  4. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  5. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  6. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  7. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

  9. Codeforces Beta Round #73 (Div. 2 Only)

    Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...

随机推荐

  1. 系统吞吐量与QPS/TPS

    QPS/TPS QPS:Queries Per Second意思是"每秒查询率",是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准. ...

  2. mysql中更改字段属性实际上都做了哪些操作

     mysql> set profiling=1; Query OK, 0 rows affected (0.00 sec) mysql> alter table test modify n ...

  3. 【Linux】系统打开文件最大数量限制(进程打开的最大文件句柄数设置)

    利用ulimit命令可以对资源的可用性进行控制. -H选项和-S选项分别表示对给定资源的硬限制(hard limit)和软限制(soft limit)进行设置. 硬限制(hard limit)一旦被设 ...

  4. Java编译期注解处理器详细使用方法

    目录 Java编译期注解处理器 启用注解处理器 遍历语法树 语法树中的源节点 语法树节点的操作 给类增加注解 给类增加import语句 构建一个内部类 使用方法 chainDots方法 总结 Java ...

  5. ubuntu20.04并添加桌面快捷方式,以安装火狐可浏览器开发版(水狐)为例

    @参考原文 1. 下载linux版源文件 从火狐官网下载linux版的水狐源文件压缩包,@火狐浏览器开发版(水狐)下载地址. 2. 解压下载源文件 将下载的"tar.bz2"文件解 ...

  6. MongoDB 总结

    目录 1. 逻辑结构 2. 安装部署 2.1 系统准备 2.2 mongodb安装 2.2.1 创建所需用户和组 2.2.2 创建mongodb所需目录结构 2.2.3 上传并解压软件到指定位置 2. ...

  7. vue首次加载白屏过渡动画(vue优化)

    过渡动画需要在index.html文件里面添加 1.css,在public.index.css创建index.css html, body, #app { height: 100%; margin: ...

  8. JavaScript中的Promise【期约】[未完成]

    JavaScript中的Promise[期约] 期约主要有两大用途 首先是抽象地表示一个异步操作.期约的状态代表期约是否完成. 比如,假设期约要向服务器发送一个 HTTP 请求.请求返回 200~29 ...

  9. javascript之Banner图片焦点轮播

    这个Banner唯一不好的就是没有前进和后退的button,写过两个版本的banner,这次这个是下面有浮动层的. <!DOCTYPE html><html xmlns=" ...

  10. 第一个 IDEA 应用程序

    新建 Java Web 项目 打开 IDEA -> Create New Project 选择 Java -> Java EE -> Web Application 选择工作空间 项 ...