-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakePalString.java
More file actions
83 lines (71 loc) · 2.2 KB
/
MakePalString.java
File metadata and controls
83 lines (71 loc) · 2.2 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
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.fqh;
import java.io.*;
import java.util.StringTokenizer;
/**
* @author ikun
* @version v1.0.0
* @since 2024/2/16 21:53
**/
public class MakePalString {
/**
* 给定一个字符串
* 求最少去掉几个字符,才能变为回文串
* case1: abcd -> 3
* case2: bcba -> 1
*/
public static void solve() throws IOException {
// dp
// s[l] == s[r] --> f[l][r] = min(f[l+1][r-1])
// s[l] != s[r] --> f[l][r] = min(f[l+1][r],f[l][r-1]) + 1
// ans -> f[0][n-1]
String s = in.nextLine();
int n = s.length();
int[][] f = new int[n][n];
for (int i = 1; i < n; i++) {
for (int l = 0, r = i; r < n; l++, r++) {
if (s.charAt(l) == s.charAt(r)) {
f[l][r] = f[l+1][r-1];
} else {
f[l][r] = Math.min(f[l+1][r], f[l][r-1]) + 1;
}
}
}
out.println(f[0][n-1]);
}
static boolean MULTI_CASE = false;
public static void main(String[] args) throws Exception {
int T = MULTI_CASE ? in.nextInt() : 1;
while (T-- > 0) {
solve();
}
out.close();
}
static InputReader in = new InputReader();
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static class InputReader {
private StringTokenizer st;
private BufferedReader bf;
public InputReader() {
bf = new BufferedReader(new InputStreamReader(System.in));
st = null;
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public String nextLine() throws IOException {
return bf.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
}