728x90
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
내가 푼 풀이
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int alphaC[] = { 1, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
for (int tc = 1; tc <= t; tc++) {
String word1 = sc.next();
String word2 = sc.next();
boolean isPossible = true;
if (word1.length() != word2.length()) {
isPossible = false;
}
else {
for (int i = 0; i < word1.length(); i++) {
if (word1.charAt(i) == 'B') {
if (word2.charAt(i) != 'B') {
isPossible = false;
break;
}
} else {
if (alphaC[word1.charAt(i) - 'A'] != alphaC[word2.charAt(i) - 'A']) {
isPossible = false;
break;
}
}
}
}
if (isPossible) {
System.out.println("#" + tc + " " + "SAME");
} else {
System.out.println("#" + tc + " " + "DIFF");
}
}
}
}
좋았던 풀이 (참고)
import java.util.Scanner;
class Solution
{
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
String noHole = "CEFGHIJKLMNSTUVWXYZ";
String oneHole = "ADOPQR";
for(int tc= 1; tc <= T; tc++)
{
String a = sc.next();
String b = sc.next();
String result = "SAME";
if(a.length() != b.length()){
result = "DIFF";
}else{
for(int i = 0; i < a.length(); i++){
String posA = String.valueOf(a.charAt(i));
String posB = String.valueOf(b.charAt(i));
if(posA.equals("B") && posB.equals("B")){
}else if(noHole.contains(posA) && noHole.contains(posB)){
}else if(oneHole.contains(posA) && oneHole.contains(posB)){
}else{
result = "DIFF";
break;
}
}
}
System.out.println("#" + tc + " " + result);
}
}
}
정석같은 풀이
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 1; tc <= t; tc++) {
String word1 = sc.next();
String word2 = sc.next();
word1=word1.replaceAll("[CEFGHIJKLMNSTUVWXYZ]", "C").replaceAll("[ADOPQR]", "A");
word2=word2.replaceAll("[CEFGHIJKLMNSTUVWXYZ]", "C").replaceAll("[ADOPQR]", "A");
System.out.println("#"+tc+" "+(word1.equals(word2)?"SAME":"DIFF"));
}
}
}728x90
'SW Expert Academy > SWEA D3' 카테고리의 다른 글
| [SW Expert Academy] 진기의 최고급 붕어빵 (D3) (0) | 2024.10.04 |
|---|---|
| [SW Expert Academy] 희성이의 원근법 (D3) (0) | 2024.10.04 |
| [SW Expert Academy] 방울 마술 (D3) (1) | 2024.10.03 |
| [SW Expert Academy] 증가하는 사탕 수열 (D3) (1) | 2024.10.03 |
| [SW Expert Academy] 0/1 Knapsack (D3) (1) | 2024.10.03 |