Share 5. longest palindromic substring c++,

#LongestPalindromicsubString, #C ++,

Một palindrom là một chuỗi giống nhau về phía sau và về phía trước.Ví dụ: "Trường đua" là một palindrom.Chất nền palindromic dài nhất của một chuỗi là chuỗi con dài nhất là một palindrom.Ví dụ, cơ sở palindromic dài nhất của "chuối" là "Anana".

Tìm kiếm cơ sở palindromic dài nhất của một chuỗi là một vấn đề cổ điển trong khoa học máy tính.Có nhiều thuật toán khác nhau để giải quyết vấn đề này, nhưng một trong những thuật toán hiệu quả nhất là thuật toán lập trình động.

Thuật toán lập trình động để tìm các chất nền palindromic dài nhất hoạt động bằng cách đầu tiên xây dựng một bảng của tất cả các palindromes có thể có chiều dài 1, 2, 3, v.v.Ví dụ: bảng cho các chuỗi có độ dài 1 sẽ chứa các mục sau:

`` `
|Chiều dài |Palindrom |
| --- | --- |
|1 |A |
|2 |aa |
`` `

Bảng cho các chuỗi có độ dài 2 sẽ chứa các mục sau:

`` `
|Chiều dài |Palindrom |
| --- | --- |
|2 |aa |
|3 |ABA |
`` `

Bảng cho các chuỗi có độ dài 3 sẽ chứa các mục sau:

`` `
|Chiều dài |Palindrom |
| --- | --- |
|3 |ABA |
|4 |Abba |
`` `

Và như thế.

Khi bảng được xây dựng, có thể tìm thấy phần phụ palindromic dài nhất của một chuỗi bằng cách bắt đầu ở đầu chuỗi và tìm kiếm palindrom dài nhất bắt đầu ở vị trí đó.Ví dụ, cơ sở palindromic dài nhất của chuỗi "chuối" là "Anana".Điều này có thể được tìm thấy bằng cách nhìn vào bảng cho các chuỗi có độ dài 4, trong đó có mục nhập "ABBA".

Thuật toán lập trình động để tìm ra chuỗi con palindromic dài nhất là một thuật toán rất hiệu quả và nó có thể được sử dụng để tìm thấy cơ sở palindromic dài nhất của một chuỗi có độ dài trong thời gian đa thức.

### Mã số

Mã sau đây thực hiện thuật toán lập trình động để tìm phần phụ palindromic dài nhất trong C ++:

`` `C ++
#include <Istream>
#include <String>

sử dụng không gian tên STD;

// Trả về chiều dài của chuỗi con palindromic dài nhất trong chuỗi đã cho.
int longestpalindrom (chuỗi s) {
// Tạo một bảng để lưu trữ độ dài của tất cả các palindromes có thể.
int n = s.length ();
Bảng int [n] [n];

// Khởi tạo bảng.
for (int i = 0; i <n; i ++) {
for (int j = 0; j <n; j ++) {
Bảng [j] = 0;
}
}

// Điền vào bảng.
for (int i = 0; i <n; i ++) {
for (int j = i; j <n; j ++) {
// Nếu các ký tự ở vị trí i và j giống nhau, thì độ dài của palindrom
// bắt đầu ở vị trí I và kết thúc ở vị trí J là 1 cộng với chiều dài của palindrom
// bắt đầu ở vị trí I + 1 và kết thúc ở vị trí J - 1.
if (s == s [j]) {
Bảng [j] = bảng [i + 1] [j - 1] + 2;
}

// Nếu không, độ dài của palindrom bắt đầu ở vị trí I và kết thúc ở vị trí j là 0.
khác {
Bảng [j] = 0;
}
}
}

// Trả về giá trị tối đa trong bảng.
int dàieStpalindromelps = 0;
for (int i = 0; i <n; i ++) {
for (int j = 0;
=======================================
#LongestPalindromicsubString, #C++, #dynamicprogramming, #String, #AlGorithM ## Longest Palindromic Substring in C++

A palindrome is a string that is the same backwards and forwards. For example, "racecar" is a palindrome. The longest palindromic substring of a string is the longest substring that is a palindrome. For example, the longest palindromic substring of "banana" is "anana".

Finding the longest palindromic substring of a string is a classic problem in computer science. There are many different algorithms for solving this problem, but one of the most efficient is the dynamic programming algorithm.

The dynamic programming algorithm for finding the longest palindromic substring works by first building up a table of all the possible palindromes of length 1, 2, 3, and so on. For example, the table for strings of length 1 would contain the following entries:

```
| Length | Palindrome |
|---|---|
| 1 | a |
| 2 | aa |
```

The table for strings of length 2 would contain the following entries:

```
| Length | Palindrome |
|---|---|
| 2 | aa |
| 3 | aba |
```

The table for strings of length 3 would contain the following entries:

```
| Length | Palindrome |
|---|---|
| 3 | aba |
| 4 | abba |
```

And so on.

Once the table is built, the longest palindromic substring of a string can be found by starting at the beginning of the string and looking for the longest palindrome that starts at that position. For example, the longest palindromic substring of the string "banana" is "anana". This can be found by looking at the table for strings of length 4, which contains the entry "abba".

The dynamic programming algorithm for finding the longest palindromic substring is a very efficient algorithm, and it can be used to find the longest palindromic substring of a string of any length in polynomial time.

### Code

The following code implements the dynamic programming algorithm for finding the longest palindromic substring in C++:

```c++
#include <iostream>
#include <string>

using namespace std;

// Returns the length of the longest palindromic substring in the given string.
int longestPalindrome(string s) {
// Create a table to store the lengths of all the possible palindromes.
int n = s.length();
int table[n][n];

// Initialize the table.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
table[j] = 0;
}
}

// Fill in the table.
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
// If the characters at positions i and j are the same, then the length of the palindrome
// that starts at position i and ends at position j is 1 plus the length of the palindrome
// that starts at position i + 1 and ends at position j - 1.
if (s == s[j]) {
table[j] = table[i + 1][j - 1] + 2;
}

// Otherwise, the length of the palindrome that starts at position i and ends at position j is 0.
else {
table[j] = 0;
}
}
}

// Return the maximum value in the table.
int longestPalindromeLength = 0;
for (int i = 0; i < n; i++) {
for (int j = 0;
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top