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. 通过trace分析优化其如何选择执行计划

    mysql5.6提供了对sql的跟踪trace,通过trace文件能够进一步了解为什么优化其选择执行计划a而不选b执行计划,帮助我们更好的理解优化其的行为. 使用方式:首先打开trace,设置格式为j ...

  2. 【ORACLE错误】SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled

    执行set autotrace traceonly的时候,报错 SQL> set autotrace traceonly SP2-0618: Cannot find the Session Id ...

  3. 解决ROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'creat table study_record( id int(11) not null

    之前一直用的好好的,突然就出现了这个错误: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual tha ...

  4. 发票校验BAPI_INCOMINGINVOICE_CREATE

    CALL FUNCTION 'BAPI_INCOMINGINVOICE_CREATE'    EXPORTING      headerdata                = headerdata ...

  5. 天天用SpringBoot居然还不知道它的自动装配的原理?

    引言 最近有个读者在面试,面试中被问到了这样一个问题"看你项目中用到了springboot,你说下springboot的自动配置是怎么实现的?"这应该是一个springboot里面 ...

  6. guava eventbus 原理+源码分析

    前言: guava提供的eventbus可以很方便的处理一对多的事件问题, 最近正好使用到了,做个小结,使用的demo网上已经很多了,不再赘述,本文主要是源码分析+使用注意点+新老版本eventbus ...

  7. jackson学习之一:基本信息

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  8. SpringCloud Alibaba Nacos注册中心源码浅析

    一.前置了解 1.1 简介 Nacos是一款阿里巴巴推出的一款微服务发现.配置管理框架.我们本次对将对它的服务注册发现功能进行简单源码分析. 1.2 流程 Nacos的分析分为两部分,一部分是我们的客 ...

  9. 【AtCoder Beginner Contest 181】A~F题解

    越学越菜系列 于2020.11.2,我绿了(错乱) A - Heavy Rotation 签到题,奇数Black,偶数White. code: #include<bits/stdc++.h> ...

  10. 转 Jmeter测试实践:文件下载接口

    Jmeter测试实践:文件下载接口   一 Jmeter步骤 1.打开jmeter4.0,新建测试计划,添加线程组.根据实际情况配置线程属性. 2.添加HTTP请求.根据接口文档进行配置. Basic ...