标签:style blog http io color os ar java for
题目来源:A Great Alchemist
Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB
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 S1, S2, S3. Determine wether Carol can create S3 from S1 and S2 or not.
The input will be given in the following format from the Standard Input.
S1 S2 S3
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.
You can make EDDAAA
by picking AAD
from the first metal, and AED
from the second metal.
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)
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 }
标签:style blog http io color os ar java for
原文地址:http://www.cnblogs.com/pinxiong/p/4068784.html