穷竭搜索: POJ 2718 Smallest Difference
题目: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的更多相关文章
- POJ 2718 Smallest Difference(最小差)
Smallest Difference(最小差) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given a numb ...
- poj 2718 Smallest Difference(暴力搜索+STL+DFS)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6493 Accepted: 17 ...
- POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)
Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- poj 2718 Smallest Difference(穷竭搜索dfs)
Description Given a number of distinct , the integer may not start with the digit . For example, , , ...
- POJ 2718 Smallest Difference 枚举
http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...
- POJ 2718 Smallest Difference【DFS】
题意: 就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小. 思路: 我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生 ...
- POJ 2718 Smallest Difference(dfs,剪枝)
枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...
- POJ - 2718 Smallest Difference(全排列)
题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举 ...
随机推荐
- Codeforces Round #299 (Div. 2) D. Tavas and Malekas kmp
题目链接: http://codeforces.com/problemset/problem/535/D D. Tavas and Malekas time limit per test2 secon ...
- virsh 操作kvm虚拟机
#查看你的硬件是否支持虚拟化.命令: [root@VM_166_143 data]#egrep '(vmx|svm)' /proc/cpuinfo #安装基础包 [root@VM_166_143 da ...
- leetcode动态规划
http://blog.csdn.net/u012162613/article/details/41428119
- Atcoder arc080E Young Maids(线段树+优先队列)
给出一个n排列,每次可以选择相邻的两个数字放在新的排列首部,问最后形成的新的排列字典序最小是? 考虑新排列的第一个数字,则应是下标为奇数的最小数,下标不妨设为i.第二个数字应该下标大于i且为偶数的最小 ...
- JAVAScript对象及初始面向对象
javaScript对象及初始面向对象 1:内置对象 例:Date String Array 类等... 2:自定义对象 方法1:var newObj=new Object(); ...
- URAL 1969. Hong Kong Tram
有一个trick就是没想到,枚举第二段时间后,要检测该火车能否继续跑一圈来判断,不能先检测前半圈能不能跑加进去后在检测后半段: // **** 部分不能放在那个位置: 最近代码导致的错误总是找不出,贴 ...
- P4596 [COCI2011-2012#5] RAZBIBRIGA
题目描述 四个等长的单词可以放在一起构成一个正方形,两个单词水平放置,两个竖直放置.水平单词只能从左往右读,竖直的单词只能从上往下读.四个角共用一个字母. 图中是由单词HLAD,NIVA,HSIN,D ...
- Windows平台下在服务中添加MySQL
widows下查看服务 1.桌面计算机-->右键-->管理-->计算机管理(本地)--->服务和应用程序-->服务 2.运行 中输入 services.msc 在服务中添 ...
- 在java中为什么要把main方法定义为一个static方法?
我们知道,在C/C++当中,这个main方法并不是属于某一个类的,它是一个全局的方法,所以当我们执行的时候,c++编译器很容易的就能找到这个main方法,然而当我们执行一个java程序的时候,因为ja ...
- 【刷题】HDU 1853 Cyclic Tour
Problem Description There are N cities in our country, and M one-way roads connecting them. Now Litt ...