题目来源: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  (N is an integer) letters long, which consists of uppercase alphabets.

Carol can create metal  from  and  alchemical when she can make the name of  by taking  letters each from  and then rearranging them properly.

You are given  names of the metal , , . Determine wether Carol can create  from  and  or not.


Input

The input will be given in the following format from the Standard Input.



  • On the first line, you will be given the name of the first metal material .
  • On the second line, you will be given the name of the second metal material .
  • On the third line, you will be given the name of the metal , which Carol wants to create.
  • Each character in the , , and  will be an uppercase English alphabet letter.
  • Each string ,  and  has same number of letters and the number is always even.
  • It is guaranteed that 

Output

If Carol can create  from  and , 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

  1. AABCCD
  2. ABEDDA
  3. EDDAAA

Output Example 1

  1. YES

You can make EDDAAA by picking AAD from the first metal, and AED from the second metal.


Input Example 2

  1. AAAAAB
  2. CCCCCB
  3. AAABCB

Output Example 2

  1. 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");
}
}
}
05-23 00:20