728x90
문제
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWl0ZQ8qn7UDFAXz
풀이
문자열의 길이가 다르면 DIFF, 같은 경우에만 비교해서 같으면 SAME 출력
근데 첫번째 코드로는 왜인지 모르겠으나 통과가 되지 않았다.
그래서 두번째 코드로 했더니 통과가 됐다.
아직도 왜그런지 모르겠다 ㅠ 혹시 보다가 알게되면 알려주세용
코드
// 7272 - 안경이 없어!
package d3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Num7272_안경이없어 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; t++) {
sb.append("#").append(t).append(" ");
st = new StringTokenizer(br.readLine());
String str1 = st.nextToken();
String str2 = st.nextToken();
if (str1.length() != str2.length()) {
sb.append("DIFF").append("\n");
break;
}
char[] charArr1 = str1.toCharArray();
char[] charArr2 = str2.toCharArray();
for (int i = 0; i < charArr1.length; i++) {
if (charArr1[i] == 'A' || charArr1[i] == 'D' || charArr1[i] == 'O' || charArr1[i] == 'P' || charArr1[i] == 'Q' || charArr1[i] == 'R') {
charArr1[i] = 'A';
} else if (charArr1[i] == 'B') {
charArr1[i] = 'B';
} else if (charArr1[i] >= 'A' && charArr1[i] <= 'Z') {
charArr1[i] = 'C';
}
if (charArr2[i] == 'A' || charArr2[i] == 'D' || charArr2[i] == 'O' || charArr2[i] == 'P' || charArr2[i] == 'Q' || charArr2[i] == 'R') {
charArr2[i] = 'A';
} else if (charArr2[i] == 'B') {
charArr2[i] = 'B';
} else if (charArr2[i] >= 'A' && charArr2[i] <= 'Z') {
charArr2[i] = 'C';
}
}
String tempStr1 = new String(charArr1);
String tempStr2 = new String(charArr2);
if (tempStr1.equals(tempStr2)) {
sb.append("SAME").append("\n");
} else {
sb.append("DIFF").append("\n");
}
}
sb.setLength(sb.length() - 1);
System.out.println(sb);
}
}
처음엔 이렇게 풀었음 (통과X)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Num7272_안경이없어 {
static String Answer;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int T = Integer.parseInt(br.readLine());
for(int t = 1; t <= T; t++)
{
st = new StringTokenizer(br.readLine());
String str1 = st.nextToken();
String str2 = st.nextToken();
int left = 0;
int right = 0;
Answer = "SAME";
for(int i = 0; i < str1.length(); i++)
{
if(str1.length() != str2.length()) {
Answer = "DIFF";
break;
}
if(str1.charAt(i) == 'B') {
left = 2;
}else if(str1.charAt(i) == 'A' || str1.charAt(i) == 'D' || str1.charAt(i) == 'P' || str1.charAt(i) == 'Q'
|| str1.charAt(i) == 'O' || str1.charAt(i) == 'R') {
left = 1;
}else {
left = 0;
}
if(str2.charAt(i) == 'B') {
right = 2;
}else if(str2.charAt(i) == 'A' || str2.charAt(i) == 'D' || str2.charAt(i) == 'P' || str2.charAt(i) == 'Q'
|| str2.charAt(i) == 'O' || str2.charAt(i) == 'R') {
right = 1;
}else {
right = 0;
}
if(left != right) {
Answer = "DIFF";
break;
}
}
System.out.println("#" + t + " " + Answer);
}
}
}
얘는 통과
728x90