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. 用Express 4和 MongoDB打造Todo List

    本篇文章主要參考自DreamersLab - 用Express和MongoDB寫一個todo list.原文的教學內容是使用Express 3.x版,在這邊做簡單Express 4.12.1的todo ...

  2. WiFi曝出安全漏洞几近“裸奔”:运营商能借机收割一波红利吗?

    ​    作为大众生活中不可或缺的基础架构,也是智能生活普及的推动性力量,运营商的重要性毋庸置疑.但无奈的是,一直以来运营商都似乎是站在了大众的"对立面".看似光鲜亮丽,但在壮观的 ...

  3. redis实现二级缓存

    缓存的作用就是降低数据库的使用率,来减轻数据库的负担.我们平常的操作一般都是查>改,所以数据库的有些查操作是重复的,如果一直使用数据库就会有负担.Mybatis也会做缓存,也会有一级缓存和二级缓 ...

  4. LeetCode 32,并不Hard的难题,解法超级经典,带你领略动态规划的精彩

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天给大家分享的是LeetCode当中的32题,这是一道Hard难度的题.也是一道经典的字符串处理问题,在接下来的文章当中,我们会详细地解读 ...

  5. vue-cli “从入门到放弃”

    主要作用:目录结构.本地调试.代码部署.热加载.单元测试 在如今前端技术飞速发展的时代,angular.js.vue.js 和 react.js 作为前端框架已经呈现出了三国鼎立的局面.作为国人若你不 ...

  6. 这些Zepto中实用的方法集

    前言 时间过得可真快,转眼间2017年已去大半有余,你就说吓不吓人,这一年你成长了多少,是否荒度了很多时光,亦或者天天向上,收获满满.今天主要写一些看Zepto基础模块时,比较实用的部分内部方法,在我 ...

  7. H5系列一、静态页面总结

    1.img父标签设置高度,如果容器没有设置高度的话,图片会默认把容器底部撑大几像素 -- 大概3px,给容器设置高度. 2.input标记换行后默认有一个间隙,设置float属性.input标记默认还 ...

  8. postgreSQL外键引用查询 查询外键被那些表占用

    根据一个表名,查询所有外键引用它的表,以及那些外键的列名key_column_usage(系统列信息表),pg_constraint(系统所有约束表) SELECT x.table_name, x.c ...

  9. xadmin安装和配置

    1.在虚拟环境pip install xadmin 2.安装完成之后在settings.py的install app里面添加xadmin和crispy_forms 3.在主项目url里面把原来的adm ...

  10. django setting文件那些事

    1.设置语言.时区 2.设置新建的用户表作为默认用户表 3.利用apps文件夹收纳app 新建python package apps,然后把app放在该文件夹下 然后setting中添加如下代码: 4 ...