Số lần xóa tối thiểu để tạo chuỗi palindrome
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 Show Đ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ụ
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
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
Đ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]) 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 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. 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++
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
123_______143_______0
123______143_______2 123_______143_______4 123_______142_______1 // palindrome. 2int #include 9
123_______143_______0
123______145_______9 123_______147_______ std; 2int std; 4123_______142_______1 // palindrome. 2int std; 9
123_______143_______0 123_______148_______5 getLevenstein(string 6
1200 122 123_______3_______03 1204 1205 123_______3_______07 123_______148_______5 1210
Java1212
1214 1215 1214 1217
1218 1219 122
123_______3_______22 int 1224 123_______3_______2
1230
124
128
1241
1248 1249 1248 1251
1256 1257 1258
122
1257 1264
1257 1268
1256 1248 1258
122
1288 1248 1290
122
1295 1248 1297 1248 1299
1248 // CPP program to find minimum deletions to make 031248 1251
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 15
1257 // CPP program to find minimum deletions to make 311257 // CPP program to find minimum deletions to make 33
122
1248 // CPP program to find minimum deletions to make 44
1257 // CPP program to find minimum deletions to make 49
1248 // CPP program to find minimum deletions to make 44
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
1229 // CPP program to find minimum deletions to make 731204 1205
123_______143_______0
Python3
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
1248 // palindrome. 21123_______142_______1 // palindrome. 33// palindrome. 16 // palindrome. 17// palindrome. 18// palindrome. 19 1248 // palindrome. 39
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
1248 // palindrome. 65// palindrome. 19 1248 // palindrome. 39
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. 91
1248 // palindrome. 19 #include 10#include 11// CPP program to find minimum deletions to make 96 1248 #include 14 |