Description

Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the resulting integer is , the integer may not start with the digit . 

For example, if you are given the digits , , , ,  and , you can write the pair of integers  and . Of course, there are many ways to form such pairs of integers:  and ,  and , etc. The absolute value of the difference between the integers in the last pair is , and it turns out that no other pair formed by the rules above can achieve a smaller difference.

Input

The first line of input contains the number of cases to follow. For each case, there is one line of input containing at least two but no more than  decimal digits. (The decimal digits are , , ..., .) No digit appears more than once in one line of the input. The digits will appear in increasing order, separated by exactly one blank space.

Output

For each test case, write on a single line the smallest absolute difference of two integers that can be written from the given digits as described by the rules above.

Sample Input

     

Sample Output


Source

给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小。

思路:

有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生的差值比当前所搜索到的结果还要大,那么就直接返回。这个剪枝就是超时与几十MS的差距

注意一点就是可能有0 与一个数字存在的情况,比如0 3,0 5等等。

其他的就比较简单了

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
using namespace std;
#define inf 1<<29
#define N 16
int a[N];
int vis_a[N],vis_b[N];
int ans;
int Anum,Bnum;
int w;
int n;
int nums[]={,,,,,};
void dfs_B(int num,int vals,int sum){ if( num> && sum*nums[Bnum-num]-vals>=ans ) return; if(num==Bnum){
ans=min(ans,abs(sum-vals));
return;
} for(int i=;i<n;i++){
if(!vis_a[i] && !vis_b[i]){
if(num== && a[i]==)
continue;
vis_b[i]=;
dfs_B(num+,vals,sum*+a[i]);
vis_b[i]=;
}
} }
void dfs_A(int num,int vals){ if(num==Anum){
w=vals;
memset(vis_b,,sizeof(vis_b));
dfs_B(,vals,);
return;
} for(int i=;i<n;i++){
if(!vis_a[i]){
if(num== && a[i]==)
continue;
vis_a[i]=;
dfs_A(num+,vals*+a[i]);
vis_a[i]=;
}
}
}
int main()
{
int t; while(scanf("%d",&t)!=EOF)
{ getchar();
while(t--){
n=;
char ch;
while((ch=getchar())!='\n'){
if(ch>='' && ch<='')
a[n++]=ch-'';
} //for(int i=0;i<n;i++)
//printf("---%d\n",a[i]);
ans=inf; Anum=n>>;
Bnum=n-Anum; dfs_A(,); if(ans!=inf)
printf("%d\n",ans);
else
printf("%d\n",w);
} }
return ;
}
 

poj 2718 Smallest Difference(穷竭搜索dfs)的更多相关文章

  1. POJ 2718 Smallest Difference(最小差)

     Smallest Difference(最小差) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given a numb ...

  2. poj 2718 Smallest Difference(暴力搜索+STL+DFS)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6493   Accepted: 17 ...

  3. POJ 2718 Smallest Difference dfs枚举两个数差最小

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19528   Accepted: 5 ...

  4. POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)

    Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...

  5. 穷竭搜索: POJ 2718 Smallest Difference

    题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1  4  5  6  8  9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...

  6. POJ 2718 Smallest Difference【DFS】

    题意: 就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小. 思路: 我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生 ...

  7. poj 3187 Backward Digit Sums(穷竭搜索dfs)

    Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...

  8. POJ 2718 Smallest Difference(dfs,剪枝)

    枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...

  9. POJ 2718 Smallest Difference 枚举

    http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...

随机推荐

  1. (转)苹果iOS开发者账号过期临时解决方法

    苹果iOS开发者账号过期临时解决办法 苹果iOS开发者账号一年的费用是99美金,作者最近由于各种原因,导致renew没能在账号过期之前支付好,所以在账号过期等待renew的期间,试了试一些非正常手段, ...

  2. 路径问题以及cookie详解

    1.路径问题: 注意 .代表执行程序的文件夹路径,在tomcat中也就是bin目录,所以要用this.getServletContext().getRealPath("/WEB-INF/cl ...

  3. 【C#正则基础】正则表达式

    1. 代表任意多个字符:(.*?)2. 代表网页里的<body>*</body>任意的标签内容,替换以后网页源码就只剩纯文本:<[^>]*>3. 代表网页中的 ...

  4. Oracle Berkeley DB Java 版

    Oracle Berkeley DB Java 版是一个开源的.可嵌入的事务存储引擎,是完全用 Java 编写的.它充分利用 Java 环境来简化开发和部署.Oracle Berkeley DB Ja ...

  5. android启动优化

    ############################################## # power on till android lock screen comes up # # get ...

  6. Android的Recovery中font_10x10.h字库文件制作

    任务是要汉化Android中的Recovery,就了解了bootable/recovery/minui/font_10x18.h这个英文字库的来历,最终汉化的时候并没有自己汉字字库,用的github上 ...

  7. OD: Register, Stack Frame, Function Reference

    几个重要的 Win32 寄存器 EIP 指令寄存器(Extended Instruction Pointer) 存放一个指针,指向下一条等待执行的指令地址 ESP 栈指针寄存器(Extended St ...

  8. Windows 右键快速运行命令行

    原文见:右键命令行 - yacper - 博客园 方法一:配置文件夹选项 1 打开人任意文件夹,[工具] --> [文件夹选项] --> [文件类型] --> [(无)资料夹] -- ...

  9. XAML 名称范围

    XAML 名称范围存储 XAML 定义的对象名称和它们的对等实例之间的关系.此概念类似于其他编程语言和技术中的术语名称范围的更广泛的含义. 定义 XAML 名称范围的方式 XAML 名称范围中的名称使 ...

  10. sql 字段字符串内容替换

    SELECT * FROM dbo.Table WHERE Name LIKE '%NYCL23%'UPDATE Table SET Name=replace(Name,'NYCL23','WYCL1 ...