atcoder之A Great Alchemist
C - 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假设array1[i]+array2[i]<array3[i],直接输出NO;
2commonS1S3为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。
import java.util.*; public class Main {
private static final int letter_count = 26; public static boolean backTracking(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 (backTracking(s3, array1, array2, count1 + 1, count2,
curIndex + 1))
return true;
array1[index]++; // 回溯
}
if (array2[index] > 0 && count2 <= s3.length() / 2) {
array2[index]--;
if (backTracking(s3, array1, array2, count1, count2 + 1,
curIndex + 1))
return true;
array2[index]++;
}
return false;
} public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.next();
String str2 = sc.next();
String str3 = sc.next();
int[] num1 = new int[letter_count];
int[] num2 = new int[letter_count];
int[] num3 = new int[letter_count];
boolean flag = true;
int commonS1S3 = 0;
int commonS2S3 = 0;
for (int i = 0; i < str1.length(); i++) {
num1[str1.charAt(i) - 'A']++;
num2[str2.charAt(i) - 'A']++;
num3[str3.charAt(i) - 'A']++; }
for (int i = 0; i < letter_count; i++) {
if (num1[i] + num2[i] < num3[i])
flag = false; commonS1S3 += Math.min(num1[i], num3[i]);
commonS2S3 += Math.min(num2[i], num3[i]);
}
if (2 * commonS1S3 < str1.length() || 2 * commonS2S3 < str1.length())
flag = false;
if (flag)
flag = backTracking(str3, num1, num2, 0, 0, 0);
if (flag)
System.out.println("YES");
else
System.out.println("No");
}
atcoder之A Great Alchemist的更多相关文章
- Atcoder ABC138
Atcoder ABC138 A .Red or Not 一道网速题. 大于3200输出原字符串,否则就输出red. #include<iostream> #include<cstd ...
- A Great Alchemist
Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB ProblemCarol is a great alchemist. In ...
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识
链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...
- AtCoder Regular Contest 082
我都出了F了……结果并没有出E……atcoder让我差4分上橙是啥意思啊…… C - Together 题意:把每个数加1或减1或不变求最大众数. #include<cstdio> #in ...
- AtCoder Regular Contest 069 D
D - Menagerie Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement Snuke, w ...
- AtCoder Regular Contest 076
在湖蓝跟衡水大佬们打的第二场atcoder,不知不觉一星期都过去了. 任意门 C - Reconciled? 题意:n只猫,m只狗排队,猫与猫之间,狗与狗之间是不同的,同种动物不能相邻排,问有多少种方 ...
- AtCoder Grand Contest 016
在雅礼和衡水的dalao们打了一场atcoder 然而窝好菜啊…… A - Shrinking 题意:定义一次操作为将长度为n的字符串变成长度n-1的字符串,且变化后第i个字母为变化前第i 或 i+1 ...
- AtCoder Beginner Contest 069【A,水,B,水,C,数学,D,暴力】
A - K-City Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement In K-city, ...
随机推荐
- NSURLSession下载和断点续传
NSURLSession是iOS7之后新的网络接口,和经常用到NSURLConnection是类似的.在程序在前台时,NSURLSession与NSURLConnection可以相互的替代.但是当用户 ...
- 申请红帽企业版Linux开发者订阅
导读 注册成为开发者计划的成员现在可以得到一套免费的 Red Hat Enterprise Linux 许可证,RHEL 开发套件将为程序员提供一个构建企业应用的稳定发展平台.红帽开发订阅成员还可以免 ...
- Oracle密码过期设置和修改密码问题
Oracle密码过期设置和修改密码问题 学习了:https://jingyan.baidu.com/article/ce09321b5608612bff858ff3.html sqlplus / as ...
- PasswordlessAPI
passwordlessapiYOURLS允许API调用的老式的方法,使用用户名和密码参数(如果你的设置是私人的,很明显).如果担心将证书发送到野外,还可以使用秘密签名令牌进行API调用.签名的令牌你 ...
- 如何使用千千静听为MP3添加专辑封面和文字信息
使用千千静听播放器打开某MP3文件,右击该文件,选择属性. 2 点击专辑封面即可添加或更换专辑封面 点击保存到文件再点击重新读取文件即可发现有效了 3 为MP3批量添加添加封面 选中播放列表的所有文件 ...
- 如何使用FLASHGOT下载网页FLASH
1 注意火狐的广告屏蔽插件可能将一些有用的东西屏蔽掉,从而无法得到广告FLASH, 2 随后即可在桌面上找到所需文件 你也可以按住A/T并单击FLASH文件(不论鼠标是否被替换为其他图形)迅雷会自动探 ...
- iOS类方法实例方法 与 self
Objective-C里面既有实例方法也类方法.类方法(Class Method) 有时被称为工厂方法(Factory Method)或者方便方法(Convenience method).工厂方法的称 ...
- 将 Shiro 作为应用的权限基础 二:shiro 认证
认证就是验证用户身份的过程.在认证过程中,用户需要提交实体信息(Principals)和凭据信息(Credentials)以检验用户是否合法.最常见的“实体/凭证”组合便是“用户名/密码”组合. 一. ...
- wepy - 安装less/sass
关于sass\less,在wepy文档里寻找 演示安装,默认已经安装了less,我们需要安装的是scss 安装less或scss yarn yarn add wepy-compiler-sass np ...
- hadoop MultipleInputs fails with ClassCastException (get fileName)
来自:http://stackoverflow.com/questions/11130145/hadoop-multipleinputs-fails-with-classcastexception F ...