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的更多相关文章

  1. 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 ...

  2. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  3. [LeetCode] Arithmetic Slices 算数切片

    A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...

  4. Puzzle 面向服务/切面(AOP/IOC)开发框架 For .Net

    Puzzle 面向服务/切面AOP开发框架 For .Net AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效 ...

  5. 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]

    [本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...

  6. HDU5456 Matches Puzzle Game(DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5456 Description As an exciting puzzle game for ...

  7. 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 ...

  8. CodeChef COUNTARI Arithmetic Progressions(分块 + FFT)

    题目 Source http://vjudge.net/problem/142058 Description Given N integers A1, A2, …. AN, Dexter wants ...

  9. 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 ...

随机推荐

  1. List.remove()的使用注意

    不使用forEach的循环 使用forEach循环 参考 今天修改一个bug,需要取一个List和一个Set的交集,使用了双重循环.想着提高循环效率,每加入一个交集中的元素,就将List中的元素删除, ...

  2. sublime安装vue插件

    1.打开sublime text 3按 Ctrl+Shift+P(相信你有单身的手速,同时按完这3个键) 2.选中上图中,框出来的内容,按下enter. 3.选择上图的第二个即:vue syntax ...

  3. 重置gitlab管理员密码

    gitlab web管理员登录密码忘记以后可以用如下方式修改密码(本演示系统为Linux-CentOS6.6): [root@localhost ~]# gitlab-rails console pr ...

  4. node--静态服务器

    1.同步读取文件 const data = fs.readFileSync('./model/mime.json');   // 这里是添加了可以正常链接其他格式文件的服务器 const http = ...

  5. http请求缓存头详解

    缓存的作用:1.减少延迟(页面打开的速度).2.降低服务器负载(先取缓存,无缓存在请求服务器,有效降低服务器的负担).3.保证稳定性(有个笑话是手机抢购时为了保证服务器的稳定性,在前端写个随机数限制百 ...

  6. C++泛化双向链表

    泛型双向链表 双向链表(doublyLinkedList.h) /******************************************************************* ...

  7. npm install、npm install --save与npm install --save-dev、npm install -g区别

    npm install X: 会把X包安装到node_modules目录中 不会修改package.json 之后运行npm install命令时,不会自动安装X npm install X –sav ...

  8. Design Patterns | 02 什么样的代码是好代码

    目录 01 - 什么是好的代码? 02 - 评价代码的标准有哪些 2.1 可维护性(maintainability) 2.2 可读性(readability) 2.3 可扩展性(extensibili ...

  9. postman小工具

    进入lmm后,做接口测试使用的是postman,以前稍微接触过,但是不是很会用,这里就自学顺便总结一下,以便以后或者能帮助到别人,如果有什么不同的意见或者有错误,请毫不客气的指出,感谢! 推荐一篇博客 ...

  10. Simulink仿真入门到精通(二) Simulink模块

    2.1 Simulink模块的组成要素 用户构建系统模型时无需直接面对成千上万行的代码,而是通过模块化图形界面以模块化的方式构建,能够使理解变得容易,让大脑减负.通过层次化模块分布将系统功能模块化,而 ...