poj2718 穷举
来源:互联网 发布:赌博网站php源码 编辑:程序博客网 时间:2024/05/20 11:47
POJ 2718 Smallest Difference
将一个数切一刀拆成两个数,两个数每一位数字的顺序都可改变,但是不能有前导0。求这两个数之差的最小值。
我使用了搜索并且避免了递归,自认为是比较好的算法。
一开始我想到的是枚举,并且很快给出了实现:
#ifndef ONLINE_JUDGE
#pragma warning(disable : 4996)
#endif
#include <iostream>
#include <string>
#include <algorithm>
using
namespace
std;
///////////////////////////SubMain//////////////////////////////////
int
main(
int
argc,
char
*argv[])
{
#ifndef ONLINE_JUDGE
freopen
(
"in.txt"
,
"r"
, stdin);
freopen
(
"out.txt"
,
"w"
, stdout);
#endif
int
n;
cin >> n;
cin.ignore();
while
(n--)
{
string all;
getline(cin, all);
all.erase(
remove
(all.begin(), all.end(),
' '
), all.end());
int
result = 0x3F3F3F3F;
int
cut = all.size() / 2;
do
{
string s1 = all.substr(0, cut);
string s2 = all.substr(cut);
if
((s1[0] ==
'0'
&& s1.size() > 1) ||
(s2[0] ==
'0'
&& s2.size() > 1)
)
{
continue
;
}
int
n1 =
atoi
(s1.c_str());
int
n2 =
atoi
(s2.c_str());
int
dif =
abs
(n1 - n2);
if
(dif < result)
{
result = dif;
}
}
while
(next_permutation(all.begin(), all.end()));
cout << result << endl;
}
#ifndef ONLINE_JUDGE
fclose
(stdin);
fclose
(stdout);
system
(
"out.txt"
);
#endif
return
0;
}
///////////////////////////End Sub//////////////////////////////////
没料到TLE,后来想了想,这里的复杂度是O((length!)n),当length = 10 的时候内层最高达到了3628800 次循环,而用例则可能是很多的。
要优化得下点功夫,起先想到改用模拟的方法,分奇偶讨论,可是这道题目是放在2.1 最基础的“穷竭搜索” 穷竭搜索这节里,目的是训练搜索算法。
于是继续走搜索的路线,上面的程序之所以慢,是因为重复计算。比如第一个字串为12,第二个字串为34这种情况和第一个字串为34,第二个字串为12这种情况被视作不同的情况。这是应当被优化的第一个点。
第二个点是string类型的全排操作比较费时,可以用位运算优化。
我看到有些人用dfs递归,感觉很别扭,代码一不好理解,二浪费栈。
我使用了我最喜欢的玩具bitset:
#ifndef ONLINE_JUDGE
#pragma warning(disable : 4996)
#endif
#include <iostream>
#include <string>
#include <algorithm>
#include <bitset>
using
namespace
std;
///////////////////////////SubMain//////////////////////////////////
int
main(
int
argc,
char
*argv[])
{
#ifndef ONLINE_JUDGE
freopen
(
"in.txt"
,
"r"
, stdin);
freopen
(
"out.txt"
,
"w"
, stdout);
#endif
int
n;
cin >> n;
cin.ignore();
while
(n--)
{
string all;
getline(cin, all);
all.erase(
remove
(all.begin(), all.end(),
' '
), all.end());
int
length = all.size();
int
cut = length / 2;
int
permute = 1 << length;
int
result = 0x3F3F3F3F;
do
{
bitset<10> used =
static_cast
<bitset<10>>(permute);
string s1, s2;
for
(
int
i = 0; i < length; ++i)
{
if
(used[i])
{
s1 += all[i];
}
else
{
s2 += all[i];
}
}
if
(s1.size() != cut)
{
continue
;
}
if
(s1[0] ==
'0'
&& s1.size() > 1)
{
continue
;
}
// s1 s2 已经被切割出来了
// 穷举它们
do
{
int
n1 =
atoi
(s1.c_str());
do
{
if
(s2[0] ==
'0'
&& s2.size() > 1)
{
continue
;
}
int
n2 =
atoi
(s2.c_str());
int
dif =
abs
(n1 - n2);
//cout << s1 << ' ' << s2 << " dif " << dif << " result: " << result << endl;
if
(dif < result)
{
result = dif;
}
}
while
(next_permutation(s2.begin(), s2.end()));
}
while
(next_permutation(s1.begin(), s1.end()));
}
while
(--permute);
cout << result << endl;
}
#ifndef ONLINE_JUDGE
fclose
(stdin);
fclose
(stdout);
system
(
"out.txt"
);
#endif
return
0;
}
///////////////////////////End Sub//////////////////////////////////
转自:http://www.hankcs.com/program/poj-2718-smallest-difference-challenge-programming-contest-2nd-edition-exercises-answers.html
很好的算法和思路!
对bitset不懂的看这里:http://blog.163.com/lixiangqiu_9202/blog/static/53575037201251121331412/
其中getline(cin,str)在VC6.0中下一行还会要去读取字符,这是VC6的一个BUG。
还有C++的remove函数,其实是将字符替换成字符串后续的字符。并不是移除,因此要配合string类的erase函数,释放后续空间,才是删除。
http://zhidao.baidu.com/link?url=Oig1CxkpjjgTYgWKGE8DOV4mqt4PH1l_AwbZIhiIf8mic8WCv7byEgRKFKlYoNEieunDIVYrruHoryQgLtQD2q
用1<<length初始化bitset,每次循环减一,相当于枚举了给出的数的1或0的所有情况。对于每一种情况每次取出一种s1的排列,在取出一种s2的排列,避免了重复。
600多ms过。
- poj2718 穷举
- POJ2718
- poj2718
- poj2718
- poj2718
- 搜索 POJ2718
- POJ2718【DFS】
- package poj2718;
- package poj2718;
- POJ2718题解
- 穷举
- 穷举
- 穷举
- 穷举
- 穷举
- POJ2718(枚举 + 贪心)
- 特殊dfs--poj2718
- poj2718 Smallest Difference
- FEST:Fast Ensembles of Sparse Trees
- 浅谈SEO学习方法和心态
- poj3468
- 黑链是什么???
- PAT 1084 Broken Keyboard
- poj2718 穷举
- 网页HTTP返回状态码说明以及问题处理
- Python篇----Requests获取网页源码(爬虫基础)
- 百度地图开发(三)之地图控制 + 定位
- EL--EL表达式语言基础
- 【DP】 HDU 2859 Phalanx
- GTK+ OR QT
- SET的用法
- 【翻译自mos文章】Windows Service Oracleremexecservice 是干什么的