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. 二十一世纪计算 | John Hopcroft:AI革命

    编者按:信息革命的浪潮浩浩汤汤,越来越多的人将注意力转向人工智能,想探索它对人类生产生活所产生的可能影响.人工智能的下一步发展将主要来自深度学习,在这个领域中,更多令人兴奋的话题在等待我们探讨:神经网 ...

  2. 算法拾遗[4]——STL用法

    主要bb一下优先队列和字符串吧. 哦还有 bitset. 优先队列 定义很容易: priority_queue<int> pq; 内部是一个堆. 基本操作 pq.top() 取堆顶元素; ...

  3. 手机预装APP“死灰复燃”,这颗“毒瘤”到底怎么了

    ​ ​ 离全新智能手机集中发布的8月底.9月初这个时间段越来越近了,iPhone 8等重磅新机也为互联网媒体贡献了足够的流量和热度.但就在大众聚焦于新机时,一个困扰很多人的问题再度冒出头--智能手机上 ...

  4. 记一次MySQL表分区操作

    最近一次日常迭代中,业务线需要对一张大表进行联合查询,查询性能可想而知,测试过程中服务接口直接响应超时,导致服务不可用,最后临时对该表进行分区操作,暂时缓解性能问题.由于是第一次操作表分区,姑且记录一 ...

  5. Spark基础全解析

    我的个人博客:https://www.luozhiyun.com/ 为什么需要Spark? MapReduce的缺陷 第一,MapReduce模型的抽象层次低,大量的底层逻辑都需要开发者手工完成. 第 ...

  6. 【读书笔记】https://source.android.google.cn/compatibility/tests?hl=en

    AuthorBlog:秋城https://www.cnblogs.com/houser0323/ Android Platform Testing This content is geared tow ...

  7. .Net Core 使用jenkins 部署到远程服务器docker

    最近一直在研究docker和kubernetes,小型的项目其实也用不到k8s,本文就是描述如何用jenkins 进行构建asp.net core 3.1项目并且发布到远程服务器docker容器中.本 ...

  8. 全差分运算放大器ADA4930的分析(2)

    前面解释了在ADA4930组成的单端转差分电路的输入电阻RIN的大小,可知当RF=RG=1KΩ的时候,RIN=1.33KΩ. 图1单端转差分电路 如图1所示,假设信号源为2V VPP的信号,信号源的内 ...

  9. 前端每日实战:131# 视频演示如何用纯 CSS 创作一把剪刀

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/GXyGpZ 可交互视频 此视频是可 ...

  10. 前端每日实战:31# 视频演示如何利用 CSS 的动画原理,创作一个乒乓球对打动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/rvgLzK 可交互视频教程 此视频 ...