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。看了同学的代码,在执行回溯前执行一些检查就能过了。哎。
#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的更多相关文章
- atcoder之A Great Alchemist
C - A Great Alchemist Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Problem Carol i ...
- A Great Alchemist 最详细的解题报告
题目来源:A Great Alchemist A Great Alchemist Time limit : 2sec / Stack limit : 256MB / Memory limit : 25 ...
- 【翻译】MongoDB指南/CRUD操作(二)
[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...
- IELTS - Word List 28
1, The lawsuit is very much o the lawyer's mind. 2, The canteen was absolutely packed. 3, Doctors di ...
- 爹地,我找到了!,15个极好的Linux find命令示例
爹地,我找到了!, 15个极好的Linux find命令示例 英文原文:Daddy, I found it!, 15 Awesome Linux Find Command Examples 标签: L ...
- .NET 使用CouchBase 基础篇
2011年2月,CouchOne和memebase合并后,改名为Couchbase,官网地址(www.couchbase.com).membase最后一个版本为1.7.2,可在Couchbase的官网 ...
- English sentence
For a better environment, we should teach our children to put litter/garbage/trash into dustbin/dust ...
- 2016.10.08,英语,《Verbal Advantage》Level1 Unit1-4
这本书学的很辛苦,总共10个Level,每个Level有5个Unit,每个Unit10个单词,实际上自己差不多一天才能学完1个Unit10个单词.(当然,一天我只能花大约1个小时左右在英语上) 而且跟 ...
- 30个实用的Linux find命令
除了在一个目录结构下查找文件这种基本的操作,你还可以用find命令实现一些实用的操作,使你的命令行之旅更加简易.本文将介绍15种无论是于新手还是老鸟都非常有用的Linux find命令 . 首先,在你 ...
随机推荐
- Swift3.0语言教程字符串与URL的数据转换与自由转换
Swift3.0语言教程字符串与URL的数据转换与自由转换 Swift3.0语言教程字符串与URL的数据转换 Swift3.0语言教程字符串与URL的数据转换与自由转换,字符串中的字符永久保存除了可以 ...
- java发送短信--httpclient方式
最近头让我写个发送短信的java程序检测BI系统,检查数据库是否有异常发送,有则发送短信到头的手机里.这里我直说httpclient方式的get请求方式,并且已经有方式的短信的接口了,所以只要再加上参 ...
- 修改WordPress中上传附件2M大小限制的方法/php+iis上传附件默认大小修改方法
在服务器上架设好WordPress后,使用过程中发现,上传附件大小有2M的限制 话说服务器就是本机,可以直接把文件拖到附件存储文件夹下,然后在需要附件的地方引用链接 可是这种落后的方法终究不是办法,还 ...
- SqlServer 事务回滚(1)
SQL事务 一.事务概念 事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是一个不可分割的工作逻辑单元.在数据库系统上执行并发操作 ...
- 【原】iOS学习之PCH文件
1. PCH文件概述 PCH文件是一种预编译头文件(一般扩展名为.PCH),是把一个工程中较稳定的代码预先编译好放在一个文件(.PCH)里.这些预先编译好的代码可以是任何的C/C++代码--甚至可以是 ...
- Java NIO之选择器Selector
在单独的线程中,检查多个通道是否可以进行IO操作. Selector创建:静态工厂方法创建 Selector selector = Selector.open(); 注册通道 channel.conf ...
- HDU 2222 & ac自动机模板
题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...
- ACM 汉诺塔(三)
汉诺塔(三) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针.印度 ...
- Code[VS]1021 玛丽卡题解
Code[VS]1021 玛丽卡题解 SPFA Algorithm 题目传送门:http://codevs.cn/problem/1021/ 题目描述 Description 麦克找了个新女朋友,玛丽 ...
- poj 1995 裸快速幂
1. poj 1995 Raising Modulo Numbers 2.链接:http://poj.org/problem?id=1995 3.总结:今天七夕,来发水题纪念一下...入ACM这个坑 ...