A Great Alchemist 最详细的解题报告
题目来源:A Great Alchemist
A Great Alchemist
Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB
Problem
Carol is a great alchemist.
In her world, each metal has a name of 2N (N is an integer) letters long, which consists of uppercase alphabets.
Carol can create metal S3 from S1 and S2 alchemical when she can make the name of S3 by taking N letters each from S1 and S2then rearranging them properly.
You are given 3 names of the metal S1, S2, S3. Determine wether Carol can create S3 from S1 and S2 or not.
Input
The input will be given in the following format from the Standard Input.
S1
S2
S3
- On the first line, you will be given the name of the first metal material S1.
- On the second line, you will be given the name of the second metal material S2.
- On the third line, you will be given the name of the metal S3, which Carol wants to create.
- Each character in the S1, S2, and S3 will be an uppercase English alphabet letter.
- Each string S1, S2 and S3 has same number of letters and the number is always even.
- It is guaranteed that 2≦|S1|≦105
Output
If Carol can create S3 from S1 and S2, output YES
, if not, output NO
in one line. Make sure to insert a line break at the end of the output.
Input Example 1
- AABCCD
- ABEDDA
- EDDAAA
Output Example 1
- YES
You can make EDDAAA
by picking AAD
from the first metal, and AED
from the second metal.
Input Example 2
- AAAAAB
- CCCCCB
- AAABCB
Output Example 2
- NO
To make AAABCB
, you have to take at least four letters from the first material. So this can't be created alchemical.
题目比较简单,我就不翻译了。
解题思路:此题只能用回溯法来做,但是有很多地方是可以剪枝的,去掉一些不必要的操作。
1、首先统计s1、s2、s3中字母'A-Z'的个数,存放在array1、array2和array3中;
2、剪枝:1)如果array1[i]+array2[i]<array3[i],直接输出NO;
2)commonS1S3为Math.min(array1[i],array3[i]) (i=0,1,...,n-1) 求和,
commonS2S3为Math.min(array2[i],array3[i]) (i=0,1,...,n-1) 求和,
如果commonS1S3和commonS2S3分别小于n/2,直接输出NO。
3、试探回溯
具体算法(java版,直接AC)
import java.util.Scanner; public class Main { private static final int letterCount = 26; public static boolean track(String s3, int[] array1, int[] array2,
int count1, int count2, int curIndex) {
if (curIndex >= s3.length()) //全部试探结束
return true;
int index = s3.charAt(curIndex) - 'A'; //curIndex所对应的下标 //如果array1[index]中没有需要的元素,同时count1(在s1中已经用掉的字符个数)小于n/2
if (array1[index] > 0 && count1 <= s3.length() / 2) {
array1[index]--; //用掉s1中一个字符
if (track(s3, array1, array2, count1 + 1, count2, curIndex + 1))
return true;
array1[index]++; //回溯
}
if (array2[index] > 0 && count2 <= s3.length() / 2) {
array2[index]--;
if (track(s3, array1, array2, count1, count2 + 1, curIndex + 1))
return true;
array2[index]++;
}
return false;
} public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s1 = scanner.next();
String s2 = scanner.next();
String s3 = scanner.next();
int[] array1 = new int[letterCount];
int[] array2 = new int[letterCount];
int[] array3 = new int[letterCount];
for (int i = 0; i < s1.length(); i++) {
array1[s1.charAt(i) - 'A']++;
array2[s2.charAt(i) - 'A']++;
array3[s3.charAt(i) - 'A']++;
}
boolean flag = true;
int commonS1S3 = 0;
int commonS2S3 = 0;
for (int i = 0; i < letterCount; i++) {
if (array3[i] > array1[i] + array2[i]) {
flag = false;
break;
}
commonS1S3 += Math.min(array1[i], array3[i]);
commonS2S3 += Math.min(array2[i], array3[i]);
}
if (commonS1S3 < s1.length() / 2 || commonS2S3 < s1.length() / 2) {
flag = false;
}
if (flag) {
flag = track(s3, array1, array2, 0, 0, 0);
}
if (flag) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
A Great Alchemist 最详细的解题报告的更多相关文章
- hihoCoder 1114 小Hi小Ho的惊天大作战:扫雷·一 最详细的解题报告
题目来源:小Hi小Ho的惊天大作战:扫雷·一 解题思路:因为只要确定了第一个是否有地雷就可以推算出后面是否有地雷(要么为0,要么为1,如果不是这两个值就说明这个方案行不通),如果两种可能中有一种成功, ...
- hihoCoder 1050 树中的最长路 最详细的解题报告
题目来源:树中的最长路 解题思路:枚举每一个点作为转折点t,求出以t为根节点的子树中的‘最长路’以及与‘最长路’不重合的‘次长路’,用这两条路的长度之和去更新答案,最终的答案就是这棵树的最长路长度.只 ...
- hihoCoder 1052 基因工程 最详细的解题报告
题目来源:基因工程 解题思路:假设基因序列长度为N,则需要计算基因序列前K个和后K个相同所需要的最少改变次数sum. 假设基因序列为 ATACGTCT (即M=8),K=6:interval=M-K= ...
- hihoCoder 1051 补提交卡 最详细的解题报告
题目来源:补提交卡 解题思路:假设未提交程序的天数为:a1,a2,....,an,补交的张数为M.依次从a1,a2,....,an中去掉连续的 K 天(0<=K<=M),然后再来计算剩余数 ...
- hihoCoder 1049 后序遍历 最详细的解题报告
题目来源:后序遍历 解题思路:开始时我只知道先通过先序.中序求出二叉树,然后再后序遍历二叉树,这当然也是一种解题思路,但是会做一些无用功,比如:计算二叉树.其实,可以直接通过先序序列和中序序列直接求出 ...
- hihoCoder 1041 国庆出游 最详细的解题报告
题目来源:国庆出游 解题思路(下面是大神的写的): 把题目中的序列称作S,树称作T.那么对于S中的任意节点x,x的子孙节点如果在S出现的话,那么这个子孙节点的位置是有一定要求的:x的所有子孙节点在S中 ...
- A Broken Calculator 最详细的解题报告
题目来源:A Broken Calculator 题目如下(链接有可能无法访问): A Broken Calculator Time limit : 2sec / Stack limit : 256M ...
- A Mountaineer 最详细的解题报告
题目来源:A Mountaineer (不知道该链接是否可以直接访问,所以将题目复制下来了) 题目如下: D - A Mountaineer Time limit : 2sec / Stack lim ...
- hihoCoder 1040 矩阵判断 最详细的解题报告
题目来源:矩阵判断 解题思路: 1.判断矩阵的4个点是否相连,一共输入8个点,只要判断是否4个点是否都经过2遍: 2.判断矩阵中任意一条边与其他边之间要么平行,要么垂直.设A(x1,y1),B(x2, ...
随机推荐
- MySQL连接查询驱动表被驱动表以及性能优化
准备我们需要的表结构和数据 两张表 studnet(学生)表和score(成绩)表, 创建表的SQL语句如下 CREATE TABLE `student` ( `id` int(11) NOT NUL ...
- linux使用组ID(SGID)共享文件
假如你有这样一个需求,一个小组内很多成员共同研究一个项目,为了这个项目我们需要分配一个具体的目录. 所有成员都拥有该目录的使用权限,可以互相操作成员的文件及内容.而且不允许其他人查看. 现在开始操作: ...
- 使用SSH远程管理时本地文件被修改了
背景: 有两个网段:1段作为工作网段即员工办公用:2段作为专用网段配置了一系列需要的环境. 在Ubuntu 16.04用Python的SSH工具在对这两个网段远程管理,我写了一个检测环境的脚本,用SF ...
- JVM源码分析之Object.wait/notify(All)完全解读
概述 本文其实一直都想写,因为各种原因一直拖着没写,直到开公众号的第一天,有朋友再次问到这个问题,这次让我静心下来准备写下这篇文章,本文有些东西是我自己的理解,比如为什么JDK一开始要这么设计,初衷是 ...
- [cpp]C++中的析构函数
C++中的析构函数 简介 析构函数(Destructors),是对象的成员函数,没有返回值也没有参数,且一个类只有一个析构函数,当对象被销毁的时候调用,被销毁通常有这么几个情况. 函数执行结束 程序执 ...
- platform驱动架构初探
platform总线是Linux2.6引入的虚拟总线,这类总线没有对应的硬件结构.与之相反,USB总线和PCI总线在内核中是有对应的bus(USB-bus和PCI-bus)的.为了统一管理CPU这些既 ...
- Android安全初学笔记
安全概述 安全主要解决4类问题 保密:不希望第三方窥探 鉴别:与你通信的人可以被确认 完整性:不能被随意篡改,或者能鉴别是否被篡改 不可否认性:能确认产生信息的人,并且产生该信息的人在何时都无法否认产 ...
- 图灵学院-微服务11-分布式链路跟踪Sleuth详解
当客户端访问到第一个service 1的时候,会生成当前链路追踪的一个全局的trance ID,在一次调用过Service1--Service2--Service3--Service4时,整个服务访问 ...
- 尚硅谷spring aop详解
spring的aop实现我们采用AspectJ的方式来实现,不采用spring框架自带的aop aspect实现有基于注解的方式,有基于xml的方式,首先我们先讲基于注解的方式,再将基于xml的方式 ...
- 代码静态测试(java)
工欲善其事,必先利其器 环境 jdk1.8 IntelliJ IDEA 1.静态代码检查 1.1工具 阿里代码规范检测工具 安装教程:阿里代码规范检查工具 1.2规范等级 在 Snoar 中对代码规则 ...