码迷,mamicode.com
首页 > 其他好文 > 详细

A Great Alchemist 最详细的解题报告

时间:2014-11-02 12:04:34      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   java   for   

题目来源: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 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 S1S2S3. 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 S1S2, and S3 will be an uppercase English alphabet letter.
  • Each string S1S2 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

  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)

bubuko.com,布布扣
 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     private static final int letterCount = 26;
 6 
 7     public static boolean track(String s3, int[] array1, int[] array2,
 8             int count1, int count2, int curIndex) {
 9         if (curIndex >= s3.length())  //全部试探结束
10             return true;
11         int index = s3.charAt(curIndex) - ‘A‘; //curIndex所对应的下标
12         
13         //如果array1[index]中没有需要的元素,同时count1(在s1中已经用掉的字符个数)小于n/2
14         if (array1[index] > 0 && count1 <= s3.length() / 2) { 
15             array1[index]--; //用掉s1中一个字符
16             if (track(s3, array1, array2, count1 + 1, count2, curIndex + 1))
17                 return true;
18             array1[index]++; //回溯
19         }
20         if (array2[index] > 0 && count2 <= s3.length() / 2) {
21             array2[index]--;
22             if (track(s3, array1, array2, count1, count2 + 1, curIndex + 1))
23                 return true;
24             array2[index]++;
25         }
26         return false;
27     }
28 
29     public static void main(String[] args) {
30         Scanner scanner = new Scanner(System.in);
31         String s1 = scanner.next();
32         String s2 = scanner.next();
33         String s3 = scanner.next();
34         int[] array1 = new int[letterCount];
35         int[] array2 = new int[letterCount];
36         int[] array3 = new int[letterCount];
37         for (int i = 0; i < s1.length(); i++) {
38             array1[s1.charAt(i) - ‘A‘]++;
39             array2[s2.charAt(i) - ‘A‘]++;
40             array3[s3.charAt(i) - ‘A‘]++;
41         }
42         boolean flag = true;
43         int commonS1S3 = 0;
44         int commonS2S3 = 0;
45         for (int i = 0; i < letterCount; i++) {
46             if (array3[i] > array1[i] + array2[i]) {
47                 flag = false;
48                 break;
49             }
50             commonS1S3 += Math.min(array1[i], array3[i]);
51             commonS2S3 += Math.min(array2[i], array3[i]);
52         }
53         if (commonS1S3 < s1.length() / 2 || commonS2S3 < s1.length() / 2) {
54             flag = false;
55         }
56         if (flag) {
57             flag = track(s3, array1, array2, 0, 0, 0);
58         }
59         if (flag) {
60             System.out.println("YES");
61         } else {
62             System.out.println("NO");
63         }
64     }
65 }
View Code

 

A Great Alchemist 最详细的解题报告

标签:style   blog   http   io   color   os   ar   java   for   

原文地址:http://www.cnblogs.com/pinxiong/p/4068784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!