Share a c++ program

Để tìm chuỗi con palindromic dài nhất ## một chương trình C ++ để tìm chuỗi con palindromic dài nhất

Một palindrom là một chuỗi giống nhau về phía sau và về phía trước.Ví dụ, "Madam" và "Racecar" là palindromes.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 "chuối" là "Anana".

Bài viết này sẽ chỉ cho bạn cách viết chương trình C ++ để tìm phần phụ palindromic dài nhất của một chuỗi.

### Thuật toán

Thuật toán để tìm ra chuỗi con palindromic dài nhất của chuỗi như sau:

1. Khởi tạo hai biến, `left` và` right`, thành 0.
2. Trong khi `left` nhỏ hơn chiều dài của chuỗi và` right` nhỏ hơn chiều dài của chuỗi:
1. Nếu các ký tự tại chỉ số `left` và` right` giống nhau:
1. Tăng `left` và` right`.
2. Nếu không:
1. Giảm `Right`.
3

### Mật mã

Mã sau thực hiện thuật toán được mô tả ở trên:

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

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

// Trả về chuỗi con palindromic dài nhất của chuỗi đã cho.
Chuỗi LongestPalindRom (String str) {
// Khởi tạo hai biến, trái và phải, 0.
int trái = 0;
int right = 0;

// Trong khi bên trái nhỏ hơn chiều dài của chuỗi và bên phải nhỏ hơn chiều dài của chuỗi:
while (trái <str.length () && right <str.length ()) {
// Nếu các ký tự ở các chỉ số trái và phải là như nhau:
if (str
== str
) {
// Tăng trái và phải.
còn lại ++;
đúng ++;
}
// Nếu không thì:
khác {
// giảm đúng.
Phải--;
}
}

// Chất nền palindromic dài nhất là phần con từ chỉ mục từ trái sang chỉ mục phải.
trả về str.substr (trái, phải - trái + 1);
}

int main () {
// Nhận chuỗi đầu vào từ người dùng.
chuỗi str;
cout << "Nhập một chuỗi:";
getline (cin, str);

// Tìm chuỗi con palindromic dài nhất của chuỗi.
Chuỗi dài nhất

// In các chuỗi con palindromic dài nhất vào bảng điều khiển.
cout << "Chất nền palindromic dài nhất là:" << longestpalindrom << endl;

trả lại 0;
}
`` `

### Chạy mã

Để chạy mã, hãy lưu nó dưới dạng `longest_palindrom.cpp` và biên dịch nó với lệnh sau:

`` `
g ++ longest_palindrom.cpp -o dài nhất_palindrom
`` `

Sau đó, chạy chương trình với lệnh sau:

`` `
./longest_palindrom
`` `

### Đầu vào và đầu ra ví dụ

**Đầu vào:**

`` `
chuối
`` `

** Đầu ra: **

`` `
Anana
`` `

### hashtags

* #xuôi ngược đều giống nhau
* #sợi dây
* #AlGorithM
* #C ++
* #Programming
=======================================
to Find the Longest Palindromic Substring ## A C++ Program to Find the Longest Palindromic Substring

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

This article will show you how to write a C++ program to find the longest palindromic substring of a string.

### The Algorithm

The algorithm for finding the longest palindromic substring of a string is as follows:

1. Initialize two variables, `left` and `right`, to 0.
2. While `left` is less than the length of the string and `right` is less than the length of the string:
1. If the characters at indices `left` and `right` are the same:
1. Increment `left` and `right`.
2. Otherwise:
1. Decrement `right`.
3. The longest palindromic substring is the substring from index `left` to index `right`.

### The Code

The following code implements the algorithm described above:

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

using namespace std;

// Returns the longest palindromic substring of the given string.
string longestPalindrome(string str) {
// Initialize two variables, left and right, to 0.
int left = 0;
int right = 0;

// While left is less than the length of the string and right is less than the length of the string:
while (left < str.length() && right < str.length()) {
// If the characters at indices left and right are the same:
if (str
== str
) {
// Increment left and right.
left++;
right++;
}
// Otherwise:
else {
// Decrement right.
right--;
}
}

// The longest palindromic substring is the substring from index left to index right.
return str.substr(left, right - left + 1);
}

int main() {
// Get the input string from the user.
string str;
cout << "Enter a string: ";
getline(cin, str);

// Find the longest palindromic substring of the string.
string longestPalindrome = longestPalindrome(str);

// Print the longest palindromic substring to the console.
cout << "The longest palindromic substring is: " << longestPalindrome << endl;

return 0;
}
```

### Running the Code

To run the code, save it as `longest_palindrome.cpp` and compile it with the following command:

```
g++ longest_palindrome.cpp -o longest_palindrome
```

Then, run the program with the following command:

```
./longest_palindrome
```

### Example Input and Output

**Input:**

```
banana
```

**Output:**

```
anana
```

### Hashtags

* #PalIndrome
* #String
* #AlGorithM
* #C++
* #Programming
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top