当前位置: 首页 > 编程日记 > 正文

哑谜,回文和暴力之美

暴力搜索是一个有趣的东西。至少刘汝佳是这么认为的。编程之美的4.10节就是典型的暴力题。虽然作者将其难度定义为一颗星,但却不能因此认为这个类型的问题就是那么容易的,很多可能需要一些有创造力的想法。

不妨试试下面这几道题:

Island of Logic:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=107&page=show_problem&problem=533

Meta-loopless sort:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=107&page=show_problem&problem=46

How big is it?:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=953

No tipping:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=109&page=show_problem&problem=1064

Addition chains:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=109&page=show_problem&problem=470

我先把其中的例题用深搜来试解一下。因为题目的解空间非常小,所以没有做什么优化。

关于节后问题:问题1,N位的总回文数为:9*pow(10, n/2 + n&1 - 1)。问题2见下面的代码。

1. 神奇的9位数:

vector<int> res;
int used[10];

void dfs(int i = 1, int v = 0) {
if (i == 10) {
res.push_back(v);
return;
}

for (int k = 1; k <= 9; ++k) {
if (used[k]) continue;
int vv = v*10 + k;
if (vv%i) continue;
used[k]
= 1;
dfs(i
+1, vv);
used[k]
= 0;
}
}

int main() {
dfs();
for (size_t i = 0; i < res.size(); ++i)
cout
<<res[i]<<'\n';
return 0;
}

2. 人过大佛寺×我=寺佛大过人以及问题2

下面的程序没有经过优化,所以会比较慢一点。另外,评估函数也只是简单满足需要。

vector<string> results;
vector
<int> tested;
vector
<int> used;

string equation;
int base;

bool satisfy(const string& s) {
char* next = 0;
int r = strtoul(s.c_str(), &next, base);
while (*next != '=') {
char op = *next++;
int t = strtoul(next, &next, base);
switch(op) {
case '+': r += t; break;
case '-': r -= t; break;
case '*': r *= t; break;
case '/': r /= t; break;
}
}

int b = strtoul(++next, 0, base);
return r == b;
}

void init(const string& str, int b = 10) {
equation
= str;
base = b;
used.assign(
base, 0);
tested.assign(
26, 0);
results.clear();
}

void dfs(size_t i = 0) {
while (i < equation.length() && !islower(equation[i])) ++i;
if (i >= equation.length()) {
if (satisfy(equation))
results.push_back(equation);
return;
}

char c = equation[i], cc = c;
if (tested[c - 'a']) return;
tested[c
- 'a'] = 1;

for (int k = (i == 0 || !isalnum(equation[i-1])); k < base; ++k) {
if (used[k]) continue;
used[k]
= 1;
char t = k < 10? '0' + k: 'A' + k - 10;
replace(equation.begin()
+ i, equation.end(), cc, t);
dfs(i
+1);
cc
= t; // for next iteration
used[k] = 0;
}
replace(equation.begin()
+ i, equation.end(), cc, c);
tested[c
- 'a'] = 0;
}

void output(ostream& os) {
if (results.size()) {
for (size_t i = 0; i < results.size(); ++i)
os
<<results[i]<<'\n';
}
else
os
<<"No solution.\n";
}

int main() {
init(
"abcde*f=edcba", 10);
dfs();
output(cout);
cout
<<'\n';

init(
"he*he=she", 10);
dfs();
output(cout);
cout
<<'\n';

init(
"he*he=she", 16);
dfs();
output(cout);

return 0;
}

转载于:https://www.cnblogs.com/acmaru/archive/2011/03/18/1987923.html

相关文章:

身份证敏感信息处理 图片添加蒙版

实现效果 需要的jar包 <!-- https://mvnrepository.com/artifact/com.jhlabs/filters --><dependency><groupId>com.jhlabs</groupId><artifactId>filters</artifactId><version>2.0.235-1</version></dependency> 调…

Linux bash管道符“|”使用介绍与例子

https://blog.csdn.net/wangqianyilynn/article/details/75576815转载于:https://www.cnblogs.com/dhName/p/10967718.html

PostgreSQL第一步:安装

PostgreSQL是一个自由的对象-关系数据库服务器&#xff08;数据库管理系统&#xff09;&#xff0c;它在灵活的BSD-风格许可证下发行。它在其他开放源代码数据库系统&#xff08;比如MySQL和Firebird&#xff09;&#xff0c;和专有系统比如Oracle、Sybase、IBM的DB2和Microsof…

【Henu ACM Round#15 A】 A and B and Chess

【链接】 我是链接,点我呀:) 【题意】 在这里输入题意 【题解】 统计大写和小写的个数。 比较答案。输出即可。 【代码】 #include <bits/stdc.h> using namespace std;string s[10]; map<char,int> dic; int inc[300];int main() {for (int i 0;i < 8;i)cin…

[转载] static class 静态类(Java)

一般情况下是不可以用static修饰类的。如果一定要用static修饰类的话&#xff0c;通常static修饰的是匿名内部类。   在一个类中创建另外一个类&#xff0c;叫做成员内部类。这个成员内部类可以静态的&#xff08;利用static关键字修饰&#xff09;&#xff0c;也可以是非静态…

tomcat监控-psi-probe使用

什么是psi-probe 这是一款 Tomcat 管理和监控工具&#xff0c;前身是 Lambda Probe。由于 Lambda Probe 2006不再更新&#xff0c;所以 PSI Probe 算是对其的一个 Fork 版本并一直更新至今。 下载 下载地址&#xff1a;https://github.com/psi-probe/psi-probe 百度网盘地址…

配置文件app.config

无论对于客户端程序还是web应用程序&#xff0c;配置文件的作用不言而喻&#xff0c;现总结用法如下&#xff1a; 1. 创建配置节类 必须创建继承自ConfigurationSection的对象才能进行配置数据读写操作&#xff0c;ConfigurationSection提供了索引器用来获取和设置配置数据&…

windows远程桌面如果超出最大连接数, 使用命令行mstsc /console登录即可

远程桌面如果超出最大连接数, 使用命令行mstsc /console登录即可。 &#xff08;也可以用 mstsc /admin&#xff09; 可以在运行里使用mstsc /console /v:IP:远程端口即可强制登录; 如果直接在远程桌面连接端使用就直接输入/console /v:IP:远程端口. 如&#xff1a;mstsc /cons…

AppiumForWin安装

尝试安装Windows版本的Appium参考&#xff1a;http://www.cnblogs.com/fnng/p/4540731.html第一步&#xff1a;安装nodehttps://nodejs.org/en/安装成功后使用&#xff1a;node -v&#xff0c;进行验证第二步&#xff1a;安装Appium下面的方法失败&#xff1a;原因下载不成功&a…

prometheus--初见

什么是prometheus Prometheus&#xff08;普罗米修斯&#xff09;是一个开源系统监控和警报工具&#xff0c;最初是在SoundCloud建立的。自2012年成立以来&#xff0c;许多公司和组织都采用了普罗米修斯&#xff0c;该项目拥有一个非常活跃的开发者和用户社区。它现在是一个独立…

JavaScript时间日期格式化

/** 时间对象的格式化;*/ Date.prototype.format function(format) {/** eg:format"YYYY-MM-dd hh:mm:ss";*/var o {"M" :this.getMonth() 1, // month"d" :this.getDate(), // day"h" :this.getHours(), // hour"m" :th…

Hello World

作为所有编程语言的起始阶段&#xff0c;HELLO WORLD占据着无法改变的地位&#xff0c;所有中/英/法/德/美……版本的编程教材中&#xff0c;HELLO WORLD总是作为第一个TEST记录于书本之中&#xff0c;所有的编程第一步就在于此了&#xff01;经典之中的经典&#xff01;HELLO …

POJ 1144 Network (求割点)

题意&#xff1a; 给定一幅无向图&#xff0c; 求出图的割点。 割点模板&#xff1a;http://www.cnblogs.com/Jadon97/p/8328750.html 分析&#xff1a; 输入有点麻烦&#xff0c; 用stringsteam 会比较简单 #include<cstdio> #include<iostream> #include<queu…

mongoose简单使用

介绍&安装 官网&#xff1a;http://www.mongoosejs.net/ npm i -S mongoose 使用 1.连接mongodb&创建模型 var mongoose require(mongoose)​//1、连接mongodb mongoose.connect(mongodb://localhost/test)​//2、设置文档结构var userSchema new mongoose.Schema…

Codeforces Round #563 (Div. 2)/CF1174

Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i1}^n a_i\)与\(\sum\limits_{n1}^{2n}a_i\)差值最大&#xff0c;排一下序就好了 CF1174B Ehab Is an Odd Person 一个显然的结论就是如果至少有一个奇数和一个偶数&#xff…

Enterprise Architect 中文经典教程

一、Enterprise Architect简介Enterprise Architect是一个对于软件系统开发有着极好支持的CASE软件&#xff08;Computer Aided Software Engineering&#xff09;。EA不同于普通的UML画图工具&#xff08;如VISIO&#xff09;&#xff0c;它将支撑系统开发的全过程。在需求分析…

[WebDev]Web 开发与设计师速查手册大全

Cheat Sheet 一词在中文中并没有很贴切的对译&#xff0c;大概是考试作弊条一类的东西&#xff0c;这要求 Cheat Sheet 必须短小精悍又覆盖广泛&#xff0c;作为 Web 开发与设计师&#xff0c;免不了在工作时查询大量资料&#xff0c;某个 Web 色值&#xff0c;某个 JavaScript…

android中的回调

1、引子 android中的回调最经典的就是点击事件设置监听&#xff08;一般通过switch&#xff08;v.getId()&#xff09;&#xff09;这里写个最主要的 btn_rigister.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View view) {// TODO log in} }…

nodejs回调函数理解

回调实例 问题&#xff1a;想要得到一秒后 计算出的结果 //错误写法function add(x,y) {console.log(1);setTimeout(function () {console.log(2);var ret x y;return ret;},1000);console.log(3)}console.log(add(10,20))添加一个函数作为参数&#xff0c;将计算出来的结果传…

C# 运算符的优先级

优先级由高到低 --(用作前缀); ; -(一元); () ; ! ; ~ * ; / ; % ; - <<; >> < ; > ; < ; > ; ! & ^ | && || ; * ; / ; % ; ; - ; << ; >> ; & ; ^ ;| --(作后缀);转载于:https://www.cnblogs.com/h…

Service Manager 的系统要求

以下各节包含有关 Service Manager 的硬件和软件要求的信息&#xff0c;并基于以下环境。System Center Service Manager 2010 已经过测试&#xff0c;并且正在使用一个支持 80 到 100 个并发 Service Manager 控制台的 Service Manager 管理服务器&#xff0c;测试根据本指南中…

读Lodash源码——chunk.js

The time is out of joint: O cursed spite, That ever I was born to set it right. --莎士比亚 最艰难的第一步 最近学习遇到了些障碍&#xff0c;浮躁浮躁又浮躁。很难静下心来做一件事&#xff0c;北京的寒风也难以让我冷静下来. 之前一直很想找个源码读读&#xff0c;太懒…

使用相对路径时,./、../、../../,代表的什么?

./ 当前目录。../ 父级目录。/ 根目录。 举个栗子&#xff1a; 页面引入js、css等文件&#xff1a; 1.如果about.jsp页面想引入common.css文件&#xff1a; 以about.jsp为基点寻找 直到 和static文件在同一级&#xff1b; 2.如果引入的外部css、js文件又引入image等时&#x…

asyncawait

简单理解 async async就是将方法变成异步 await 是等待异步方法的执行完成&#xff0c;可以获取异步方法里面的数据&#xff0c;但必须得用在异步方法(async)里面 创建异步方法 定义一个普通方法&#xff0c;返回值是一个字符串 function getData() {return 这是一个数据;}co…

引起路由器重启的“元凶”

文章出处&#xff1a;www.net1980.com在我们的日常维护中&#xff0c;偶然会遇到一些路由器自动重启的故障。那么大家都会自然地猜测到是否设备已经运行一段长时间&#xff0c;设备稳定性开始减低。或者可能有工作人员把电源拨松了&#xff0c;又或者设备负荷过重等原因。那么究…

python csv.reader参数指定

转载于:https://www.cnblogs.com/mahailuo/p/8375997.html

xBIM 实战01 在浏览器中加载IFC模型文件

系列目录 【已更新最新开发文章&#xff0c;点击查看详细】 一、创建Web项目打开VS&#xff0c;新建Web项目&#xff0c;选择 .NET Framework 4.5选择一个空的项目新建完成后&#xff0c;项目结构如下&#xff1a; 二、添加webServer访问文件类型由于WexXplorer 加载的是 .w…

node 判断文件夹是否存在

判断文件夹是否存在 let filePath path.join(__dirname,../)/download_tmp/fs.exists(filePath, function(exists) {if(!exists){fs.mkdir(filePath,function (err) {if(err){console.log(err)}})}});生成excle文件到本地 业务要求&#xff1a;生成excle文件到本地的路径 #安装…

IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)

以下代码在IE8下运行通过&#xff0c;在IE9中出错&#xff1a;document.createElement(<iframe id"yui-history-iframe" src"../../images/defaults/transparent-pixel.gif" style"position:absolute;top:0;left:0;width:1px;height:1px;visibilit…

数字家庭开发者中心

数字家庭开发者中心 http://www.adobe.com/devnet/devices/digital_home.html转载于:https://www.cnblogs.com/kobo/archive/2010/07/06/1772136.html