Share leetcode 8 c++,

quynhhoarooster

New member
#LeetCode #C ++ #Coding #Programming #algorithms ## LeetCode 8: C ++ Giải pháp

**Vấn đề:**

Đưa ra một chuỗi s, tìm chuỗi con dài nhất là palindrom.

**Ví dụ:**

`` `
Đầu vào: "Babad"
Đầu ra: "Bab"
Giải thích: "ABA" cũng là một palindrom, nhưng nó ngắn hơn "Bab".
`` `

**Giải pháp:**

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

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

// một chức năng tiện ích để kiểm tra xem một chuỗi nhất định có phải là palindrom không
bool ispalindrom (chuỗi S)
{
int n = s.length ();
for (int i = 0; i <n / 2; i ++)
if (s ! = s [n - i - 1])
trả lại sai;
trả lại đúng;
}

// một chức năng để tìm chuỗi con palindrom dài nhất
Chuỗi dài nhất
{
int n = s.length ();

// trường hợp cơ sở
if (n == 0 || n == 1)
trả lại s;

// dp [j] lưu trữ đúng nếu cơ sở s [i..j] là palindrom
bool dp [n] [n];

// khởi tạo tất cả các giá trị là sai
for (int i = 0; i <n; i ++)
for (int j = 0; j <n; j ++)
dp [j] = false;

// Tất cả các chuỗi con có độ dài 1 là palindromes
for (int i = 0; i <n; i ++)
dp = true;

// Kiểm tra palindromes có độ dài 2
for (int i = 0; i <n - 1; i ++)
if (s == s [i + 1])
dp [i + 1] = true;

// Kiểm tra palindromes có chiều dài 3 trở lên
for (int i = 0; i <n - 2; i ++) {
for (int j = i+2; j <n; j ++) {
// Nếu hai ký tự giữa là giống nhau và
// Chất nền ở bên trái và bên phải của giữa
// Hai ký tự là palindromes, sau đó toàn bộ
// Chất nền là một palindrom
if (s == s [j] && dp [i + 1] [j - 1]))
dp [j] = true;
}
}

// Tìm chuỗi con palindrom dài nhất
int start = 0, maxLpm = 0;
for (int i = 0; i <n; i ++) {
for (int j = i; j <n; j ++) {
if (dp [j] && j - i + 1> maxlength) {
bắt đầu = i;
MAXLEPT = J - I + 1;
}
}
}

// Trả lại cơ sở palindrom dài nhất
trả về s.substr (bắt đầu, maxlength);
}

int main ()
{
Chuỗi s = "Babad";
cout << longestpalindrom (s) << endl;
trả lại 0;
}
`` `

## hashtags:

* #LeetCode
* #C ++
* #mã hóa
* #Programming
* #algorithms
=======================================
#LeetCode #C++ #Coding #Programming #algorithms ## Leetcode 8: C++ Solution

**Problem:**

Given a string s, find the longest substring that is palindrome.

**Example:**

```
Input: "babad"
Output: "bab"
Explanation: "aba" is also a palindrome, but it is shorter than "bab".
```

**Solution:**

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

using namespace std;

// A utility function to check if a given string is palindrome
bool isPalindrome(string s)
{
int n = s.length();
for (int i = 0; i < n / 2; i++)
if (s != s[n - i - 1])
return false;
return true;
}

// A function to find the longest palindrome substring
string longestPalindrome(string s)
{
int n = s.length();

// Base cases
if (n == 0 || n == 1)
return s;

// dp[j] stores true if substring s[i..j] is palindrome
bool dp[n][n];

// Initialize all values as false
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
dp[j] = false;

// All substrings of length 1 are palindromes
for (int i = 0; i < n; i++)
dp = true;

// Check for palindromes of length 2
for (int i = 0; i < n - 1; i++)
if (s == s[i + 1])
dp[i + 1] = true;

// Check for palindromes of length 3 and more
for (int i = 0; i < n - 2; i++) {
for (int j = i + 2; j < n; j++) {
// If the middle two characters are the same and the
// substrings on the left and right of the middle
// two characters are palindromes, then the entire
// substring is a palindrome
if (s == s[j] && dp[i + 1][j - 1])
dp[j] = true;
}
}

// Find the longest palindrome substring
int start = 0, maxLength = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (dp[j] && j - i + 1 > maxLength) {
start = i;
maxLength = j - i + 1;
}
}
}

// Return the longest palindrome substring
return s.substr(start, maxLength);
}

int main()
{
string s = "babad";
cout << longestPalindrome(s) << endl;
return 0;
}
```

## Hashtags:

* #LeetCode
* #C++
* #Coding
* #Programming
* #algorithms
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top