728x90
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.util.ArrayList;
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++) {
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<String> list = new ArrayList<>();
int ans = 0;
boolean isExist = false;
for (int i = 0; i < n; i++) {
list.add(sc.next());
}
//System.out.println(list);
for (int i = 0; i < list.size(); i++) {
String word = list.get(i);
//System.out.println(word);
if (isPalindrome(word)) {
isExist = true;
}
String reverse = "";
for (int j = 0; j < word.length(); j++) {
reverse += word.charAt(word.length() - 1 - j);
}
// System.out.println(reverse);
for (int z = i + 1; z < list.size(); z++) {
if (list.get(z).equals(reverse)) {
ans += word.length() * 2;
list.remove(z);
list.remove(i);
i=-1;
break;
}
}
//System.out.println(list);
}
if (isExist)
ans += m;
System.out.println("#" + tc + " " + ans);
}
//
}
public static boolean isPalindrome(String str) {
int start = 0;
int end = str.length() - 1;
while (start <= end) {
if (str.charAt(start) != str.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}
}
※ 팰린드롬이 여러 개라도 써먹을 수 있는 건 하나 뿐이다.
728x90
'SW Expert Academy > SWEA D3' 카테고리의 다른 글
| [SW Expert Academy] 세상의 모든 팰린드롬 (D3) (0) | 2024.09.19 |
|---|---|
| [SW Expert Academy] 회문의 회문 (D3) (0) | 2024.09.19 |
| [SW Expert Academy] 순열1 (D3) (0) | 2024.09.19 |
| [SW Expert Academy] 육십갑자 (D3) (0) | 2024.09.19 |
| [SW Expert Academy] 한빈이와 Spot Mart (D3) (0) | 2024.09.19 |