Share codeforces 71a c++,

dang66666666

New member
#CodeForces, #71a, #C ++, #algorithms, #Probol giải quyết ## CodeForces 71a C ++ Solution

Trong bài viết này, chúng tôi sẽ giải quyết CodeForces 71A, một vấn đề từ phần C ++ của vấn đề CodeForce.Vấn đề như sau:

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

Một palindrom là một chuỗi giống nhau về phía sau và về phía trước.
`` `

Chúng ta có thể giải quyết vấn đề này bằng cách sử dụng phương pháp lập trình động đơn giản.Chúng tôi xác định một bảng `dp [j]` là độ dài của palindrom dài nhất kết thúc tại index `i` và bắt đầu tại index` j`.Sau đó, chúng ta có thể điền vào bảng theo kiểu từ dưới lên, bắt đầu với các trường hợp cơ sở `dp = 1` với tất cả` i`.

Đối với mỗi `i> j`, chúng ta có thể kiểm tra xem cơ sở có phải không [i ... j]` là một palindrom.Nếu là, thì chúng ta đặt `dp [j] = dp [i - 1] [j + 1] + 2`.Mặt khác, chúng tôi đặt `dp [j] = 0`.

Khi chúng tôi đã điền vào bảng, chúng tôi có thể tìm thấy palindrom dài nhất bằng cách tìm giá trị tối đa trong bảng.Các chỉ số bắt đầu và kết thúc của palindrom dài nhất có thể được tìm thấy bằng cách tìm hai chỉ số `i` và` j` sao cho `dp [j] = max (dp [j])`.

Sau đây là việc triển khai C ++ của thuật toán trên:

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

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

int main () {
dây;
cin >> s;

int n = s.length ();

int dp [n] [n];
for (int i = 0; i <n; i ++) {
for (int j = 0; j <n; j ++) {
dp [j] = 0;
}
}

for (int i = 0; i <n; i ++) {
dp = 1;
}

for (int i = 1; i <n; i ++) {
for (int j = 0; j <i; j ++) {
if (s == s [j] && (i - j <= 2 || dp [j + 1] [i - 1]> 0)) {
dp [j] = dp [j + 1] [i - 1] + 2;
}
}
}

int maxlen = 0;
int start = 0;
int end = 0;
for (int i = 0; i <n; i ++) {
for (int j = 0; j <n; j ++) {
if (dp [j]> maxlen) {
MaxLen = dp [j];
bắt đầu = i;
kết thúc = j;
}
}
}

cout << s.substr (bắt đầu, kết thúc - bắt đầu + 1) << endl;

trả lại 0;
}
`` `

Giải pháp này chạy trong không gian O (N^2) và O (N^2).

## hashtags

* #CodeForces
* #71a
* #C ++
* #algorithms
* #Giải quyết vấn đề
=======================================
#CodeForces, #71a, #C++, #algorithms, #Problem Solving ## CodeForces 71A C++ Solution

In this article, we will solve CodeForces 71A, a problem from the C++ section of the Codeforces problemset. The problem is as follows:

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

A palindrome is a string that is the same backwards and forwards.
```

We can solve this problem using a simple dynamic programming approach. We define a table `dp[j]` to be the length of the longest palindrome that ends at index `i` and starts at index `j`. We can then fill in the table in a bottom-up fashion, starting with the base cases `dp = 1` for all `i`.

For each `i > j`, we can check if the substring `s[i...j]` is a palindrome. If it is, then we set `dp[j] = dp[i - 1][j + 1] + 2`. Otherwise, we set `dp[j] = 0`.

Once we have filled in the table, we can find the longest palindrome by finding the maximum value in the table. The starting and ending indices of the longest palindrome can be found by finding the two indices `i` and `j` such that `dp[j] = max(dp[j])`.

The following is a C++ implementation of the above algorithm:

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

using namespace std;

int main() {
string s;
cin >> s;

int n = s.length();

int dp[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[j] = 0;
}
}

for (int i = 0; i < n; i++) {
dp = 1;
}

for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (s == s[j] && (i - j <= 2 || dp[j + 1][i - 1] > 0)) {
dp[j] = dp[j + 1][i - 1] + 2;
}
}
}

int maxLen = 0;
int start = 0;
int end = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dp[j] > maxLen) {
maxLen = dp[j];
start = i;
end = j;
}
}
}

cout << s.substr(start, end - start + 1) << endl;

return 0;
}
```

This solution runs in O(n^2) time and O(n^2) space.

## Hashtags

* #CodeForces
* #71a
* #C++
* #algorithms
* #Problem-solving
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top