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 S2 then 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|≦100000
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.

用回溯法TLE。看了同学的代码,在执行回溯前执行一些检查就能过了。哎。

 #include <iostream>
#include <string>
#include <vector>
using namespace std; bool backtrack(string &S3, int charsFromS1, int charsFromS2, int current,
vector<int> &charsInS1, vector<int> &charsInS2) {
if (current >= S3.length()) return true;
char index = S3[current] - 'A';
if (charsInS1[index] > && charsFromS1 < S3.length() / ) {
charsInS1[index]--;
if (backtrack(S3, charsFromS1 + , charsFromS2, current + , charsInS1, charsInS2)) return true;
charsInS1[index]++;
}
if (charsInS2[index] > && charsFromS2 < S3.length() / ) {
charsInS2[index]--;
if (backtrack(S3, charsFromS1, charsFromS2 + , current + , charsInS1, charsInS2)) return true;
charsInS2[index]++;
}
return false;
} int main(int argc, char** argv) {
string S1, S2, S3;
cin >> S1 >> S2 >> S3;
vector<int> charsInS1(, ), charsInS2(, ), charsInS3(, ); for (int i = ; i < S1.length(); ++i) {
charsInS1[S1[i] - 'A']++;
charsInS2[S2[i] - 'A']++;
charsInS3[S3[i] - 'A']++;
} int common13 = , common23 = ;
for (int i = ; i < ; ++i) {
if (charsInS3[i] > charsInS1[i] + charsInS2[i]) {
cout << "NO" << endl;
return ;
}
common13 += min(charsInS3[i], charsInS1[i]);
common23 += min(charsInS3[i], charsInS2[i]);
} if (common13 < S3.length() / || common23 < S3.length() / ) {
cout << "NO" << endl;
} else {
bool ans = backtrack(S3, , , , charsInS1, charsInS2);
cout << (ans ? "YES" : "NO") << endl;
}
return ;
}

A Great Alchemist的更多相关文章

  1. atcoder之A Great Alchemist

    C - A Great Alchemist Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Problem Carol i ...

  2. A Great Alchemist 最详细的解题报告

    题目来源:A Great Alchemist A Great Alchemist Time limit : 2sec / Stack limit : 256MB / Memory limit : 25 ...

  3. 【翻译】MongoDB指南/CRUD操作(二)

    [原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...

  4. IELTS - Word List 28

    1, The lawsuit is very much o the lawyer's mind. 2, The canteen was absolutely packed. 3, Doctors di ...

  5. 爹地,我找到了!,15个极好的Linux find命令示例

    爹地,我找到了!, 15个极好的Linux find命令示例 英文原文:Daddy, I found it!, 15 Awesome Linux Find Command Examples 标签: L ...

  6. .NET 使用CouchBase 基础篇

    2011年2月,CouchOne和memebase合并后,改名为Couchbase,官网地址(www.couchbase.com).membase最后一个版本为1.7.2,可在Couchbase的官网 ...

  7. English sentence

    For a better environment, we should teach our children to put litter/garbage/trash into dustbin/dust ...

  8. 2016.10.08,英语,《Verbal Advantage》Level1 Unit1-4

    这本书学的很辛苦,总共10个Level,每个Level有5个Unit,每个Unit10个单词,实际上自己差不多一天才能学完1个Unit10个单词.(当然,一天我只能花大约1个小时左右在英语上) 而且跟 ...

  9. 30个实用的Linux find命令

    除了在一个目录结构下查找文件这种基本的操作,你还可以用find命令实现一些实用的操作,使你的命令行之旅更加简易.本文将介绍15种无论是于新手还是老鸟都非常有用的Linux find命令 . 首先,在你 ...

随机推荐

  1. chrome插件

    自备FQ神器,或者在公司浏览谷歌商店.话说我们公司电脑可以打开谷歌商店. 1.Performance-Analyser(网页性能分析) 这款插件是用来分析你的网页加载性能的,包括http请求,执行期的 ...

  2. 17243 Huzi酱和他的俄罗斯套娃(贪心)

    时间限制:500MS  内存限制:65535K 提交次数:15 通过次数:4 收入:12 题型: 编程题   语言: C++;C Description Huzi酱是个非常贪玩的人,除了魔方他还喜欢各 ...

  3. 【CLR in c#】事件

    1.事件模型建立在委托的基础上. 2,定义事件编译器会做三个动作,第一个构造具有委托类型的字段,事件发生时会通知这个列表中的委托. 第二个构造的是一个添加关注的方法+=.  第三个构造是一个注销事件关 ...

  4. jquery笔记(操作HTML)

    获取: $("selector").text(): 获取元素里面的文本 $("selector").html(): 获取元素里面的代码标签 $("se ...

  5. Android一体式(沉浸式)状态栏的实现

    注:公司开发任务适配是在4.4版本之上进行,所以此适配仅在4.4之上进行测试. 1.主要使用了第三方的开源项目SystemBarTint,github:https://github.com/jgilf ...

  6. Experimental Educational Round: VolBIT Formulas Blitz

    cf的一次数学场... 递推 C 题意:长度<=n的数只含有7或8的个数 分析:每一位都有2种可能,累加不同长度的方案数就是总方案数 组合 G 题意:将5个苹果和3个梨放进n个不同的盒子里的方案 ...

  7. BZOJ3583 : 杰杰的女性朋友

    将$I$转置,设$G=OI$,则$ans=G^0+G^1+...+G^d$. 注意到$G^d=O(IO)^{d-1}I$,而$IO$是大小为$k\times k$的矩阵,可以通过倍增在$O(k^3\l ...

  8. POJ1419 & 最大团

    题意: 求一个图的最大点独立集.SOL: 转化为补图的最大团,最大团似乎是一个NP问题,那么只好爆搜了. 补一补图论基础,代码不想打了,来自某blog #include <iostream> ...

  9. BZOJ3172[Tjoi2013]单词 题解

    题目大意: 求一些字符串在一段文章中出现的次数. 思路: AC自动机的经典应用,建完自动机直接将队列里的元素调Fail指针记录即可. 代码: #include<cstdio> #inclu ...

  10. 20145308刘昊阳 《Java程序设计》第6周学习总结

    20145308刘昊阳 <Java程序设计>第6周学习总结 教材学习内容总结 第10章 输入/输出 10.1 InputStream与OutputStream 10.1.1 串流设计概念 ...