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 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。看了同学的代码,在执行回溯前执行一些检查就能过了。哎。

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 using namespace std;
 5
 6 bool backtrack(string &S3, int charsFromS1, int charsFromS2, int current,
 7         vector<int> &charsInS1, vector<int> &charsInS2) {
 8     if (current >= S3.length()) return true;
 9     char index = S3[current] - ‘A‘;
10     if (charsInS1[index] > 0 && charsFromS1 < S3.length() / 2) {
11         charsInS1[index]--;
12         if (backtrack(S3, charsFromS1 + 1, charsFromS2, current + 1, charsInS1, charsInS2)) return true;
13         charsInS1[index]++;
14     }
15     if (charsInS2[index] > 0 && charsFromS2 < S3.length() / 2) {
16         charsInS2[index]--;
17         if (backtrack(S3, charsFromS1, charsFromS2 + 1, current + 1, charsInS1, charsInS2)) return true;
18         charsInS2[index]++;
19     }
20     return false;
21 }
22
23 int main(int argc, char** argv) {
24     string S1, S2, S3;
25     cin >> S1 >> S2 >> S3;
26     vector<int> charsInS1(26, 0), charsInS2(26, 0), charsInS3(26, 0);
27
28     for (int i = 0; i < S1.length(); ++i) {
29         charsInS1[S1[i] - ‘A‘]++;
30         charsInS2[S2[i] - ‘A‘]++;
31         charsInS3[S3[i] - ‘A‘]++;
32     }
33
34     int common13 = 0, common23 = 0;
35     for (int i = 0; i < 26; ++i) {
36         if (charsInS3[i] > charsInS1[i] + charsInS2[i]) {
37             cout << "NO" << endl;
38             return 0;
39         }
40         common13 += min(charsInS3[i], charsInS1[i]);
41         common23 += min(charsInS3[i], charsInS2[i]);
42     }
43
44     if (common13 < S3.length() / 2 || common23 < S3.length() / 2) {
45         cout << "NO" << endl;
46     } else {
47         bool ans = backtrack(S3, 0, 0, 0, charsInS1, charsInS2);
48         cout << (ans ? "YES" : "NO") << endl;
49     }
50     return 0;
51 }
时间: 2024-10-22 07:29:38

A Great Alchemist的相关文章

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 alphabet

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

[words]2017.7.20

affiliate vt. The affiliated high school of Beijing Normal University is affiliated with BNU. aggravate vt. The bad grades I got from that class really aggravated me. Your confort will not work and can only aggravate the situation. agitation n. Becau

【分享】VNR翻译日语游戏汉化简易图解教材

请[点击图片]到新链接看[原图].不然博客自动缩小图,看不清图解. 上面是用美少女万花镜来测试新版VNR翻译的如何,结果比我预料还要好.以前旧版根本比不上新版的.翻译非常准确.看了我上面的简易VNR图解,应该了解了怎样翻译了吧.接下来就是D.C.III.RX翻译. 来看下翻译效果吧. 最新版文本设置,其它还都是一样. D.C.III RX在VNR下全屏化 如果出现部分打开GAL游戏VNR却不自动弹出翻译窗口和翻译不出文本,请看下面解决方法. 提取文本后无法翻译或翻译不完整,不通顺解决方法 D.C

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

[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关注),写确认(写关注) 1 更新文档 1.1 更新 MongoDB提供下列方法用于更新一个集合 db.collection.updateOne() 更新使用指定过滤器匹配到的文档,即使过滤器匹配到多个文档,也只会更新一个文档. 3.2版本新增特性. db.collection.updateMany(

SWERC13 Decoding the Hallway

找规律 S+1 = S +'L'+~rev(S) Problem D Decoding the Hallway Problem D Edward is now 21 years old. He has to appear in an exam to renew his State Alchemist title. This year the exam is arranged in a bit different way. There will be a long hallway. Each al

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

爹地,我找到了!, 15个极好的Linux find命令示例 英文原文:Daddy, I found it!, 15 Awesome Linux Find Command Examples 标签: Linux 523人收藏此文章, 我要收藏66号公路 推荐于 3年前 (共 9 段, 翻译完成于 09-27) (20评) 参与翻译(3人): 一刀, 和雨冰风, hylent 仅中文 | 中英文对照 | 仅英文 | 打印此文章 前阵子,我们审查了15件实事 find命令的例子(第一部分).查找命令可

什么是真正的程序员

什么是真正的程序员 这篇文章的原文来自:A Little Printf Story作者仿照<小王子>中的情节,通过小printf遇见的不同类型的程序员,最后悟出什么才是真正的程序员!第一次翻译有很多不妥,欢迎留言指正. 文章略长,但是耐心读完,你肯定会受益良多! 第一章 (推荐看完整篇文章,再回过头看一遍第一章) 我非常幸运出生在一个电脑和电子游戏还没有普遍的时代.所以我可以和我的小伙伴们一起玩耍,同时发明属于我们的游戏. 我们十分会玩:用树枝做成'???'.我们可以用树枝做出任何东西,除'回

sed教程(八)之Sed字符串

本教程将介绍一些字符串处理的重要sed命令.考虑我们有一个文本文件books.txt 要处理,它有以下内容: 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage