andy6804tw
3/29/2017 - 4:18 AM

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=437 這題只是數值比對有四種情況 1.A equals B (兩串列相等) 2.

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=437

這題只是數值比對有四種情況 1.A equals B (兩串列相等) 2.A is a proper subset of B(A是B的子集) 3.B is a proper subset of A(B是A的子集) 4.A and B are disjoint(兩串列完全不同,沒交集) 5.I'm confused!(有相等的數字但不構成子集,如{1,2} {2,3} 或是 {1,2,3} {3,4} )

這題建議不要使用uDebug的測資因為內有例外測資,簡單來說就是沒有重複數字的集合 NOTES: The judge's input most likely does not contain any negative integers and sets like so 3 3 3 2 3 3 3 3 3 3 2 However, both cases are handled correctly by the code for this problem on uDebug.

import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		while (scn.hasNext()) {
			String str1[] = scn.nextLine().split(" "), str2[] = scn.nextLine().split(" ");
			int arr1[] = new int[str1.length], arr2[] = new int[str2.length];
			for (int i = 0; i < arr1.length; i++)
				arr1[i] = Integer.parseInt(str1[i]);
			for (int i = 0; i < arr2.length; i++)
				arr2[i] = Integer.parseInt(str2[i]);
			Arrays.sort(arr1);
			Arrays.sort(arr2);
			if (arr1.length == arr2.length) { // 先判斷兩串列依樣長時
				int i = 0;
				int count = 0;
				for (i = 0; i < arr1.length; i++) {
					for (int j = 0; j < arr2.length; j++) {
						if (arr1[i] == arr2[j]) {
							count++;
						}
					}
				}
				if (count == arr1.length)
					System.out.println("A equals B");
				else if (count == 0)
					System.out.println("A and B are disjoint");
				else
					System.out.println("I'm confused!");

			} else if (arr1.length > arr2.length) {// 當arr1(A)長度比arr2(B)長時
				int count = 0;
				for (int i = 0; i < arr1.length; i++) {
					for (int j = 0; j < arr2.length; j++) {
						if (arr1[i] == arr2[j]) {
							count++;
							break;
						}
					}
				}
				if (count == arr2.length)
					System.out.println("B is a proper subset of A");
				else if (count > 0)
					System.out.println("I'm confused!");
				else
					System.out.println("A and B are disjoint");

			} else if (arr1.length < arr2.length) {// 當arr1(A)長度比arr2(B)短時
				int count = 0;
				for (int i = 0; i < arr2.length; i++) {
					for (int j = 0; j < arr1.length; j++) {
						if (arr2[i] == arr1[j]) {
							count++;
							break;
						}
					}
				}
				if (count == arr1.length)
					System.out.println("A is a proper subset of B");
				else if (count > 0)
					System.out.println("I'm confused!");
				else
					System.out.println("A and B are disjoint");

			}
		}
	}
	/* 
    題目:Q496 : Simply Subsets
    作者:1010
    時間:西元 2017 年 3 月 28 日*/
}