-
Notifications
You must be signed in to change notification settings - Fork 0
/
5502.java
51 lines (34 loc) · 1.2 KB
/
5502.java
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
import java.io.*;
import java.util.*;
public class Main {
static class Node{
String str;
int cnt;
public Node(String str, int cnt) {
this.str = str;
this.cnt = cnt;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();
sb.append(st.nextToken());
StringBuilder reverse = new StringBuilder(sb).reverse();
int[][] dp = new int[N + 1][N + 1];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (sb.charAt(i - 1) == reverse.charAt(j - 1)) {
dp[i][j] = dp[i-1][j-1] + 1;
} else{
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
bw.write(N - dp[N][N]+"\n");
bw.flush();
}
}