题目http://poj.org/problem?id=2718

题意

就是输入N组数据,一组数据为,类似 【1  4  5  6  8  9】这样在0~9之间升序输入的数据,然后从这些数据中切一刀,比如  n1:【1 4 5】,n2:【6 8 9】这样,然后 abs(n1- n2),对n1 和 n2的所有可能的排列 n1: 【1 4 5】【1 5 4】...这样,要算出来的最小的差,显然从中间切一刀才会出现这种解。

题解

这里可以用来练习 STL,算法不会也没有关系,可以在这题学到 bitset 的用法,类模板 bitset 表示一个 N 位的固定大小序列。可以用标准逻辑运算符操作位集,并将它与字符串和整数相互转换。如:bitset<10> used = static_cast<bitset<10>>(permute),10个二进制位, permute表示这个数的二进制表示形式;比如:permute=3; used就是 0000000011; used[0] = used[1] = 1; 这个低位在右边 

我们这样,设输入字符串长度 len, 我们遍历 2 ^ len种可能,  used[i] == 1,则 s1 += line[i] ;否则 s2 += line[i];这样避免了  s1 = "1 4 5", s2 = "6 8 9" 或 s1 = "6 8 9", s2 = "1 4 5" 这种重复的情况。这样 s1, s2就被切割出来了。再对 s1, s2字符串的位置进行搜索, s1 = "123", "132"....这样

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <bitset>
using namespace std; int T;
const int INF = ; void solve()
{
scanf("%d", &T);
getchar(); //下面输入 带空格的字符串, 需要吃掉输入T的回车,再可以
while (T--)
{
string line;
getline(cin, line);
line.erase(remove(line.begin(), line.end(), ' '), line.end()); //擦除' '
int ans = INF;
int half = line.size() / ;
int len = line.size();
int permute = << len; // 2 ^ len ; do {
//10个二进制位, permute表示这个数的二进制表示形式; permute=3; used就是 0000000011;
bitset<> used = static_cast<bitset<>>(permute); // used[0] = used[1] = 1; 这个低位在右边
// cout << "Debug: " << used << endl; //可以输出used看看
string s1, s2;
for (int i = ; i < len; i++)
{
if (used[i])
{
s1 += line[i];
}
else
{
s2 += line[i];
}
}
if ((s1.size() != half) || (s1[] == '' && s1.size() > ))
{
continue;
}
//s1 s2已经被切割出来了
//s1, s2字符串的位置进行搜索, s1 = "123", "132"....这样
do
{
int n1 = atoi(s1.c_str());
do {
if (s2[] == '' && s2.size() > ) {
continue;
}
int n2 = atoi(s2.c_str());
int diff = abs(n1 - n2);
if (diff < ans) {
ans = diff;
}
} while (next_permutation(s2.begin(), s2.end()));
} while(next_permutation(s1.begin(), s1.end()));
} while (--permute); printf("%d\n", ans);
} } int main()
{
solve(); return ;
}

穷竭搜索: POJ 2718 Smallest Difference的更多相关文章

  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(贪心 or next_permutation暴力枚举)

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

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

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

  5. poj 2718 Smallest Difference(穷竭搜索dfs)

    Description Given a number of distinct , the integer may not start with the digit . For example, , , ...

  6. POJ 2718 Smallest Difference 枚举

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

  7. POJ 2718 Smallest Difference【DFS】

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

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

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

  9. POJ - 2718 Smallest Difference(全排列)

    题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举 ...

随机推荐

  1. git 提交本地文件,删除文件夹,修改文件等

    1. 下载git工具包 链接: https://git-scm.com/download/win 2. 右键打开git bash 登陆到自己的github账户 $ git config --globa ...

  2. 【CSAPP笔记】10. 代码优化

    写程序的主要目标是使它在所有可能的情况下都能正确运行(bug free),一个运行得很快但有 bug 的程序是毫无用处的.在 bug free 的基础上,程序员必须写出清晰简洁的代码,这样做是为了今后 ...

  3. Ubuntu16.04 下虚拟环境的创建与使用

    1. 虚拟环境   虚拟环境(virtual environment),顾名思义是虚拟出来的环境,通俗来讲,可以借助虚拟机,docker来理解虚拟环境,就是把一部分内容独立出来,我们把这部分独立出来的 ...

  4. Windows上MyEclipse2017 CI7 安装、破解以及配置

    一.安装环境与安装包 操作系统:win7 MyEclipse2017 CI7下载地址:链接:https://pan.baidu.com/s/1TWkwntF9i5lOys3Z96mpLQ MyEcli ...

  5. 实测 | 转型微服务,这4大工具谁是API网关性能最优?

    转自:http://www.servicemesh.cn/?/article/45 作者:Turgay Çelik 翻译:钟毅(Drew Zhong) 原文:Comparing API Gateway ...

  6. L2 L3 L4

    第二层交换机,是根据第二层数据链路层的MAC地址和通过站表选择路由来完成端到端的数据交换的.因为站表的建立与维护是由交换机自动完成,而路由器又是属于第三层设备,其寻址过程是根据IP地址寻址和通过路由表 ...

  7. log4j2分析总结(一)

    现在公司用log4j2 进行日志记录,我也看了相关的资料,现在进行记录学习总结下 整体结构 Appenders里设置日志的输出方式.级别和格式 Loggers里设置全局的级别和绑定appenders里 ...

  8. HDU2883_kebab

    很好的题目. 有不多于200个任务,每个任务要在si到ei这个时间段内完成,每个任务的任务量是ti*ni,只有一台机器,且其单位时间内可完成的任务量为m. 现在问你,能否使所有的任务全部在规定的时间段 ...

  9. 内存映像分析工具Eclipse Memory Analyzer

    1. Eclipse Memory Analyzer安装 Help ->Eclipse Marketplace,搜索Memory,点击install,->confirm->同意证书内 ...

  10. ansible部署(pip安装)

    centos7 pip安装 ansible 首先ansible基于python2.X 环境 默认centos都已经安装好了python2环境 安装可选性 ansible可以通过源码,yum,pip等方 ...