Verbal Arithmetic Puzzle
2020-01-02 12:09:09
问题描述:

问题求解:
这个问题不就是小学奥数题么?都知道要暴力枚举,但是如何巧妙的枚举才是问题的关键。在打比赛的时候,我用了全排列算法,TLE了。
借鉴了别人的解法,确实实现的要优雅很多,因此在此做一个记录。
public int[] pow_num = new int[]{1, 10, 100, 1000, 10000, 100000, 1000000};
public boolean isSolvable(String[] words, String result) {
Set<Character> seen = new HashSet<>();
boolean[] isFirst = new boolean[128];
int[] char_count = new int[128];
for (String word : words) {
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (!isFirst[c] && i == 0 && word.length() > 1) isFirst[c] = true;
char_count[c] += pow_num[word.length() - i - 1];
seen.add(c);
}
}
for (int i = 0; i < result.length(); i++) {
char c = result.charAt(i);
if (!isFirst[c] && i == 0 && result.length() > 1) isFirst[c] = true;
char_count[c] -= pow_num[lresult.length() - i - 1];
seen.add(c);
}
char[] chs = new char[seen.size()];
int idx = 0;
for (char c : seen) chs[idx++] = c;
return helper(char_count, isFirst, chs, new boolean[10], 0, 0);
}
private boolean helper(int[] char_count, boolean[] isFirst, char[] chs, boolean[] used, int step, int diff) {
if (step == chs.length) return diff == 0;
for (int i = 0; i < 10; i++) {
char c = chs[step];
if (used[i] || (i == 0 && isFirst[c])) continue;
used[i] = true;
if (helper(char_count, isFirst, chs, used, step + 1, diff + char_count[c] * i)) return true;
used[i] = false;
}
return false;
}
Verbal Arithmetic Puzzle的更多相关文章
- List of NP-complete problems
This is a list of some of the more commonly known problems that are NP-complete when expressed as de ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Arithmetic Slices 算数切片
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- Puzzle 面向服务/切面(AOP/IOC)开发框架 For .Net
Puzzle 面向服务/切面AOP开发框架 For .Net AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效 ...
- 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]
[本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...
- HDU5456 Matches Puzzle Game(DP)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5456 Description As an exciting puzzle game for ...
- Codeforces Round #342 (Div. 2) D. Finals in arithmetic(想法题/构造题)
传送门 Description Vitya is studying in the third grade. During the last math lesson all the pupils wro ...
- CodeChef COUNTARI Arithmetic Progressions(分块 + FFT)
题目 Source http://vjudge.net/problem/142058 Description Given N integers A1, A2, …. AN, Dexter wants ...
- one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏
one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...
随机推荐
- 服务治理与RPC · 跬步
以前写过Django中使用zerorpc的方法,但是由于我们的Django是运行在gevent下,而zeromq需要启动一个后台进程处理消息,与gevent使用的greenlet携程是冲突的. 在Ja ...
- Linux USB 鼠标驱动程序详解(转)
Linux USB 鼠标驱动程序详解 USB 总线引出两个重要的链表!一个 USB 总线引出两个重要的链表,一个为 USB 设备链表,一个为 USB 驱动链表.设备链表包含各种系统中的 USB 设备以 ...
- 记一个 Base64 有关的 Bug
本文原计划写两部分内容,第一是记录最近遇到的与 Base64 有关的 Bug,第二是 Base64 编码的原理详解.结果写了一半发现,诶?不复杂的一个事儿怎么也要讲这么长?不利于阅读和理解啊(其实是今 ...
- SpringCloud Ribbon组成和负载均衡规则
Ribbon饥饿加载 默认情况下Ribbon是懒加载的.当服务起动好之后,第一次请求是非常慢的,第二次之后就快很多. 解决方式:开启饥饿加载 ribbon: eager-load: enabled: ...
- 前阿里数据库专家总结的MySQL里的各种锁(上篇)
0.前言 MySQL按照加锁的范围,分为全局锁.表级锁.行级锁. 本文作为上篇,主要介绍MySQL的全局锁 和 表级锁. 重要的实战总结为,如何安全地变更一个表的表结构. 1.全局锁 定义: 全局锁就 ...
- mac中如何复制拷贝文件或文件夹的路径
copy path in Mac 首先打开 automator(自动操作) 然后 最后command+s保存,命名为copy path. 然后随便找个文件右击,选择服务,再选择 copy path , ...
- 前端开发: webStorm手机真机调试
目录 1. 做好准备工作 2. 开始设置 3. 配置路径 4. 匹配路径设置 5. 设置完成,即可体验 文章首发微信公众号飙码人生 2018-05-21 18:21:15 1. 做好准备工作 安装we ...
- 一文快速入门Shell脚本_了解Sheel脚本基本命令
通过代码和注释的形式,列举了shell的基础操作,快速入门.shell在线编辑器 注释 单行用#号:多行::<<' 多行注释... '.:<<a 多行注释... a.:< ...
- Java的三魂七魄 —— 高级多线程
目录 Java的三魂七魄 -- 高级多线程 一.多线程的创建 二.线程安全问题 三.线程通信问题 四.更多实例 1.用线程同步的方法解决单例模式的线程安全问题 2.银行存钱问题(线程安全问题) 3.生 ...
- webapck之多页面打包(常见)
webpack多入口打包 let path = require('path'); elt HtmlWebpackPlugin = require('html-webpack-plugin'); mod ...