Nếu chúng ta được cung cấp một chuỗi S, chúng ta phải tìm ra số lượng ký tự tối thiểu cần xóa để biến chuỗi thành một bảng màu. Một palindrome là một chuỗi giống nhau nếu đi qua cả từ trái sang phải và phải sang trái
Điều này có thể được thực hiện trong thời gian O[N2] với sự trợ giúp của phương pháp Lập trình động của Chuỗi con Palindromic dài nhất
Một số ví dụ về palindrome là. thưa bà, giới thiệu, Malayalam, v.v.
Chuỗi ví dụ
Input : ABBABBD
Output: 2
Vì chữ A đầu tiên và chữ D cuối cùng bị loại bỏ nên chuỗi kết quả sẽ là BBABB, đây là một bảng màu
Input : NITIN
Output: 0
Bởi vì nó đã là một palindrome không cần phải xóa bất kỳ không. của các nhân vật hơn nữa
Giải thích thuật toánCó nhiều cách để thực hiện tác vụ này nhưng cách tối ưu nhất là tìm dãy con palindrome dài nhất có thể được tạo thành từ chuỗi đã cho và trừ nó khỏi chuỗi ban đầu sẽ cho chúng ta không có ký tự nào bị xóa để tạo chuỗi a
Chuỗi con palindrom dài nhất có thể được tìm thấy bằng cách sử dụng phương pháp Lập trình động trong thời gian O[N2]. Bạn nên xem qua bài viết này tại OpenGenus để hiểu cách tiếp cận. Đây là điều cơ bản
Ý tưởng là sử dụng đệ quy để giải quyết vấn đề này. Ý tưởng là so sánh ký tự cuối cùng của chuỗi X[i…j]
với ký tự đầu tiên của nó. Có hai khả năng
- Nếu ký tự cuối cùng của chuỗi giống với ký tự đầu tiên thì không cần xóa và chúng tôi lặp lại cho chuỗi con còn lại ________ 137 _______
- Nếu ký tự cuối cùng của chuỗi khác với ký tự đầu tiên, hãy trả về một cộng với giá trị nhỏ nhất trong hai giá trị mà chúng tôi nhận được bằng cách
- Xóa ký tự cuối cùng và đệ quy cho chuỗi con còn lại
X[i, j-1]
- Xóa ký tự đầu tiên và đệ quy cho chuỗi con còn lại
X[i+1, j]
- Xóa ký tự cuối cùng và đệ quy cho chuỗi con còn lại
Điều này mang lại mối quan hệ đệ quy sau
T[i…j] =. T[i+1…j-1] [nếu X[i] = X[j]]
. 1 + phút [T[i+1…j], T[i…j-1]] [nếu X[i]. = X[j]]
Thuật toán có thể được triển khai như sau trong C++, Java và Python. Nó tìm số lần xóa tối thiểu cần thiết để chuyển đổi một chuỗi X
thành một bảng màu theo cách đệ quy bằng cách sử dụng các mối quan hệ trên
Cho một chuỗi A, hãy tính số ký tự tối thiểu bạn cần xóa để biến chuỗi kết quả thành một bảng màu
ví dụ.
Input : baca Output : 1 Input : geek Output : 2
Khuyến khích. Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp
Chúng tôi đã thảo luận về một cách tiếp cận trong bài dưới đây.
Số lần xóa tối thiểu để tạo một chuỗi palindrome
Cách tiếp cận dưới đây sẽ sử dụng khoảng cách Levenshtein đã sửa đổi. Chúng tôi xem xét Levenshtein đã sửa đổi [chỉ xem xét xóa] cả chuỗi gốc và đảo ngược của nó
C++
// CPP program to find minimum deletions to make
// palindrome.
#include
using
namespace
std;
int
getLevenstein[string
___
120
121
122
123_______3_______4
123_______3_______6
123_______3_______8
123______141_______0
123_______147_______
// CPP program to find minimum deletions to make
3123_______141_______5
int
// CPP program to find minimum deletions to make
7int
141_______9123_______142_______1
// palindrome.
2int
// palindrome.
4// palindrome.
5_______142_______6
// palindrome.
5// palindrome.
8
123_______143_______0
123______143_______2
123_______143_______4
123_______142_______1
// palindrome.
2int
#include
9// palindrome.
5_______142_______1 // palindrome.
2int
using
4
using
5_______144_______6 using
7
using
8_______144_______9
using
5_______145_______1
using
8____145_______3
// palindrome.
5#include
0
123_______143_______0
123______145_______9
123_______147_______
std;
2int
std;
4123_______142_______1
// palindrome.
2int
std;
9// palindrome.
5int
1
// palindrome.
5_______144_______6 int
4
using
5int
6
// palindrome.
5_______144_______6 int
9
using
5getLevenstein[string
1
123_______143_______0
123_______148_______5
getLevenstein[string
6#include
0
getLevenstein[string
8
int
1200
122
123_______3_______03
1204
1205
123_______3_______07
123_______148_______5
1210
#include
0
Java
1212
// palindrome.
1214
1215
1214
1217
1218
1219
122
123_______3_______22
int
1224
123_______3_______2
// palindrome.
5_______3_______28____3_______29
1230
// palindrome.
5
124
// palindrome.
5______3_______34
// palindrome.
5
128
// palindrome.
5// CPP program to find minimum deletions to make
0
// palindrome.
5_______147_______
1241
// palindrome.
5_______147_______3_______44_______3_______29 int
3_______47
1248
1249
1248
1251
// palindrome.
5_______142_______1 // palindrome.
2int
1256
1257
1258
// palindrome.
5
122
using
5_______3_______62
1257
1264
using
5_______3_______66
1257
1268
// palindrome.
5#include
0
// palindrome.
5#include
2
// palindrome.
5_______143_______4
// palindrome.
5_______142_______1 // palindrome.
2int
1256
1248
1258
// palindrome.
5
122
using
5_______142_______1 // palindrome.
2int
1288
1248
1290
using
5
122
using
8_______144_______6
1295
1248
1297
1248
1299
// CPP program to find minimum deletions to make
00// CPP program to find minimum deletions to make
01
1248
// CPP program to find minimum deletions to make
031248
1251
using
8_______145_______1
// CPP program to find minimum deletions to make
00// CPP program to find minimum deletions to make
09
1248
// CPP program to find minimum deletions to make
111248
// CPP program to find minimum deletions to make
131248
// CPP program to find minimum deletions to make
15using
5#include
0
// palindrome.
5#include
0
// palindrome.
5// CPP program to find minimum deletions to make
21
// palindrome.
5_______147_______ // CPP program to find minimum deletions to make
24
// palindrome.
5_______142_______1 // palindrome.
2int
// CPP program to find minimum deletions to make
29
1257
// CPP program to find minimum deletions to make
311257
// CPP program to find minimum deletions to make
33// palindrome.
5
122
using
5_______141_______37
using
5_______144_______6 int
4
using
8_______141_______42
1248
// CPP program to find minimum deletions to make
44using
5_______144_______6 // CPP program to find minimum deletions to make
47
1257
// CPP program to find minimum deletions to make
49using
8_______141_______51
1248
// CPP program to find minimum deletions to make
44// palindrome.
5#include
0
// palindrome.
5_______148_______5 getLevenstein[string
6
123_______143_______0
123_______141_______62
123_______141_______64
1222
// CPP program to find minimum deletions to make
66 // CPP program to find minimum deletions to make
67123_______3_______2
// palindrome.
5_______141_______71
1229
// CPP program to find minimum deletions to make
731204
1205
// palindrome.
5// CPP program to find minimum deletions to make
77
123_______143_______0
#include
0
// CPP program to find minimum deletions to make
81
// CPP program to find minimum deletions to make
82
Python3
// CPP program to find minimum deletions to make
83
// CPP program to find minimum deletions to make
84
// CPP program to find minimum deletions to make
85// CPP program to find minimum deletions to make
86 // CPP program to find minimum deletions to make
87
// CPP program to find minimum deletions to make
88 // CPP program to find minimum deletions to make
89
123_______141_______91
123_______141_______93
// CPP program to find minimum deletions to make
86 // CPP program to find minimum deletions to make
95// CPP program to find minimum deletions to make
961248
// CPP program to find minimum deletions to make
98
123_______142_______00
123______142_______02
123_______142_______04
// CPP program to find minimum deletions to make
86 // palindrome.
06// palindrome.
07123_______142_______09
// CPP program to find minimum deletions to make
86 // palindrome.
11// CPP program to find minimum deletions to make
961248
// palindrome.
1 // palindrome.
15// palindrome.
16 // palindrome.
17// palindrome.
18// palindrome.
19 1248
// palindrome.
21// palindrome.
22// palindrome.
1 // palindrome.
24// palindrome.
16 // palindrome.
17// palindrome.
18// palindrome.
19
1248
// palindrome.
21123_______142_______1
// palindrome.
33// palindrome.
16 // palindrome.
17// palindrome.
18// palindrome.
19 1248
// palindrome.
39// palindrome.
5_______3_______62____3_______57// palindrome.
43// CPP program to find minimum deletions to make
86 // palindrome.
33
// palindrome.
5_______3_______66
1257
// CPP program to find minimum deletions to make
98// CPP program to find minimum deletions to make
86 // palindrome.
33
123_______142_______53
123_______142_______55
123_______142_______57
123_______142_______1
// palindrome.
33// palindrome.
16 // palindrome.
17// palindrome.
21248
// palindrome.
65// palindrome.
19 1248
// palindrome.
39// palindrome.
5_______142_______1 // palindrome.
71// palindrome.
16 // palindrome.
17// palindrome.
2
1248
// palindrome.
65// palindrome.
19 1248
// palindrome.
39using
5_______144_______6 // palindrome.
82_______141_______96
1248
// CPP program to find minimum deletions to make
98// CPP program to find minimum deletions to make
86_______141_______86 // palindrome.
88// CPP program to find minimum deletions to make
96 1248
// palindrome.
91using
8_______142_______93// CPP program to find minimum deletions to make
86
1248
// palindrome.
19 #include
10#include
11// CPP program to find minimum deletions to make
96 1248
#include
14