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

Đ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án

Có 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

  1. 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 _______
  2. 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]

Đ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___

12
0
12
1

12
2

12
3_______3_______4

12
3_______3_______6

 

12
3_______3_______8

12
3______141_______0

12
3_______147_______ // CPP program to find minimum deletions to make3

12
3_______141_______5int// CPP program to find minimum deletions to make7int141_______9

12
3_______142_______1 // palindrome.2int // palindrome.4

// palindrome.5_______142_______6

// palindrome.5// palindrome.8

12
3_______143_______0

 

12
3______143_______2

12
3_______143_______4

12
3_______142_______1 // palindrome.2int #include 9

// palindrome.5_______142_______1 // palindrome.2int using4

using5_______144_______6 using7

using8_______144_______9

using5_______145_______1

using8____145_______3

// palindrome.5#include 0

12
3_______143_______0

 

12
3______145_______9

12
3_______147_______ std;2intstd;4

12
3_______142_______1 // palindrome.2int std;9

// palindrome.5int1

// palindrome.5_______144_______6 int4

using5int6

// palindrome.5_______144_______6 int9

using5getLevenstein[string1

12
3_______143_______0

12
3_______148_______5 getLevenstein[string6

#include 0

 

getLevenstein[string8

int

12
00

12
2

12
3_______3_______03
12
04
12
05

12
3_______3_______07

12
3_______148_______5
12
10

#include 0

Java




12
12

// palindrome.

12
14
12
15

12
14
12
17

 

12
18
12
19

12
2

 

12
3_______3_______22 int
12
24

12
3_______3_______2

// palindrome.5_______3_______28____3_______29

12
30

 

// palindrome.5

12
4

// palindrome.5______3_______34

 

// palindrome.5

12
8

// palindrome.5// CPP program to find minimum deletions to make0

// palindrome.5_______147_______

12
41

// palindrome.5_______147_______3_______44_______3_______29 int3_______47

12
48
12
49
12
48
12
51

// palindrome.5_______142_______1 // palindrome.2int

12
56
12
57
12
58

// palindrome.5

12
2

using5_______3_______62

12
57
12
64

using5_______3_______66

12
57
12
68

// palindrome.5#include 0

 

// palindrome.5#include 2

// palindrome.5_______143_______4

// palindrome.5_______142_______1 // palindrome.2int

12
56
12
48
12
58

// palindrome.5

12
2

using5_______142_______1 // palindrome.2int

12
88
12
48
12
90

using5

12
2

using8_______144_______6

12
95
12
48
12
97
12
48
12
99

// CPP program to find minimum deletions to make00// CPP program to find minimum deletions to make01

12
48// CPP program to find minimum deletions to make03
12
48
12
51

using8_______145_______1

// CPP program to find minimum deletions to make00// CPP program to find minimum deletions to make09

12
48 // CPP program to find minimum deletions to make11
12
48// CPP program to find minimum deletions to make13
12
48// CPP program to find minimum deletions to make15

using5#include 0

// palindrome.5#include 0

 

// palindrome.5// CPP program to find minimum deletions to make21

// palindrome.5_______147_______ // CPP program to find minimum deletions to make24

// palindrome.5_______142_______1 // palindrome.2int // CPP program to find minimum deletions to make29

12
57// CPP program to find minimum deletions to make31
12
57// CPP program to find minimum deletions to make33

// palindrome.5

12
2

using5_______141_______37

using5_______144_______6 int4

using8_______141_______42

12
48// CPP program to find minimum deletions to make44

using5_______144_______6 // CPP program to find minimum deletions to make47

12
57// CPP program to find minimum deletions to make49

using8_______141_______51

12
48// CPP program to find minimum deletions to make44

// palindrome.5#include 0

// palindrome.5_______148_______5 getLevenstein[string6

12
3_______143_______0

 

12
3_______141_______62

12
3_______141_______64
12
22 // CPP program to find minimum deletions to make66 // CPP program to find minimum deletions to make67

12
3_______3_______2

// palindrome.5_______141_______71

12
29 // CPP program to find minimum deletions to make73
12
04
12
05

// palindrome.5// CPP program to find minimum deletions to make77

12
3_______143_______0

#include 0

 

// CPP program to find minimum deletions to make81

// CPP program to find minimum deletions to make82

Python3




// CPP program to find minimum deletions to make83

// CPP program to find minimum deletions to make84

// CPP program to find minimum deletions to make85// CPP program to find minimum deletions to make86 // CPP program to find minimum deletions to make87

 

// CPP program to find minimum deletions to make88 // CPP program to find minimum deletions to make89

 

12
3_______141_______91

12
3_______141_______93// CPP program to find minimum deletions to make86 // CPP program to find minimum deletions to make95// CPP program to find minimum deletions to make96
12
48// CPP program to find minimum deletions to make98

 

12
3_______142_______00

12
3______142_______02

12
3_______142_______04// CPP program to find minimum deletions to make86 // palindrome.06// palindrome.07

12
3_______142_______09// CPP program to find minimum deletions to make86 // palindrome.11// CPP program to find minimum deletions to make96
12
48 // palindrome.1 // palindrome.15// palindrome.16 // palindrome.17// palindrome.18// palindrome.19
12
48// palindrome.21

// palindrome.22// palindrome.1 // palindrome.24// palindrome.16 // palindrome.17// palindrome.18// palindrome.19

12
48// palindrome.21

12
3_______142_______1 // palindrome.33// palindrome.16 // palindrome.17// palindrome.18// palindrome.19
12
48// palindrome.39

// palindrome.5_______3_______62____3_______57// palindrome.43// CPP program to find minimum deletions to make86 // palindrome.33

// palindrome.5_______3_______66

12
57// CPP program to find minimum deletions to make98// CPP program to find minimum deletions to make86 // palindrome.33

 

12
3_______142_______53

12
3_______142_______55

12
3_______142_______57

12
3_______142_______1 // palindrome.33// palindrome.16 // palindrome.17// palindrome.2
12
48// palindrome.65// palindrome.19
12
48// palindrome.39

// palindrome.5_______142_______1 // palindrome.71// palindrome.16 // palindrome.17// palindrome.2

12
48// palindrome.65// palindrome.19
12
48// palindrome.39

using5_______144_______6 // palindrome.82_______141_______96

12
48// CPP program to find minimum deletions to make98// CPP program to find minimum deletions to make86_______141_______86 // palindrome.88// CPP program to find minimum deletions to make96
12
48// palindrome.91

using8_______142_______93// CPP program to find minimum deletions to make86

12
48 // palindrome.19 #include 10#include 11// CPP program to find minimum deletions to make96
12
48#include 14

Chủ Đề