-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ1254.java
More file actions
70 lines (55 loc) · 1.78 KB
/
BOJ1254.java
File metadata and controls
70 lines (55 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package quki.algorithm.dp;
import java.util.Arrays;
import java.util.Scanner;
public class BOJ1254 {
/**
* Memoization하면서 return
*
* @return 1: 팰린드룸, 0: 팰린드룸X, -1: 아직 Memoization안한 지점
*/
public static int isPelindrome(int start, int end, String line, int d[][]) {
if (d[start][end] != -1)
return d[start][end];
if (start == end)
return d[start][end] = 1;
if (start + 1 == end) {
if (line.charAt(start) == line.charAt(end)) {
return d[start][end] = 1;
} else {
return d[start][end] = 0;
}
}
if (line.charAt(start) == line.charAt(end)) {
return d[start][end] = isPelindrome(start + 1, end - 1, line, d);
} else {
return d[start][end] = 0;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
int n = line.length();
int d[][] = new int[2 * n - 1][2 * n - 1];
for (int i = 0; i < 2 * n - 1; i++) {
Arrays.fill(d[i], -1);
}
if (isPelindrome(0, n-1, line, d) == 1) {
System.out.println(line.length());
} else {
int point = n-1;
for(int i = 0 ; i<=n-1;i++){
if(isPelindrome(i, n-1, line, d) == 1){
point = i;
break;
}
}
for (int i = point-1; i >= 0; i--) {
line = line + line.charAt(i);
if (isPelindrome(0, line.length() - 1, line, d) == 1) {
System.out.println(line.length());
break;
}
}
}
}
}