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 ...
随机推荐
- Django中使用websocket并实现简易聊天室
django使用websocket并实现简易聊天室 django默认只支持http协议 如果你想让django即支持http协议又支持websocket协议,则需要做以下配置 前期配置 前提需要安装c ...
- C语言链表的基本操作
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- windows 10 右键菜单注册表位置
1. 查找 1.1. 打开注册表 # 1. 使用快捷键打开 “运行” win + r # 2. 在 “运行” 中输入 regedit # 3. 回车 1.2. 点击 查找 # 1. 方法 1 : 点击 ...
- 7-9 jmu-python-异常-学生成绩处理专业版 (25 分)
小明在帮助老师统计成绩,老师给他的是一组数据.数据的第1行代表学生数n,后面的n行代表每个学生的成绩.成绩是整数类型.小明编写了一个程序,该程序可以批量处理数据,统计所有学生的平均分.当数据没有任何错 ...
- 用 Java 实现人脸识别功能(附源码)
整理了一些Java方面的架构.面试资料(微服务.集群.分布式.中间件等),有需要的小伙伴可以关注公众号[程序员内点事],无套路自行领取 更多优选 一口气说出 9种 分布式ID生成方式,面试官有点懵了 ...
- 从输入URL到页面展示
当我们输入 URL 并按回车后,浏览器会对 URL 进行检查,首先判断URL格式,比如是ftp http ed2k等等,我们这里假设这个URL是http://hellocassie.cn,那么浏览器会 ...
- 提高开发效率之VS Code基础配置篇
背景 之前一直是只用WebStorm作为IDE来编写代码,但是由于: 手中的这台Mac接了两个显示器以后,使用WebStorm会有卡顿. WebStorm需要付费(虽然可以通过某方法和谐). 所以需要 ...
- 零基础HTML及CSS编码总结
任务目的 针对设计稿样式进行合理的HTML架构,包括以下但不限于: * 掌握常用HTML标签的含义.用法 能够基于设计稿来合理规划HTML文档结构 理解语义化,合理地使用HTML标签来构建页面 掌握基 ...
- 为什么要使用webpack?
在网页中会引用到哪些常见的静态资源? js (.js .jsx .coffee .ts) css (.css .less .sass .scss scss是sass的plus版) imag ...
- 快速排序python实现总结
背景:数据结构与算法是IT相关的工程师一直以来的基础考察重点,很多经典书籍都是用c++或者java来实现,出于对python编码效率的喜爱,于是取search了一下python的快排实现,发现大家写的 ...