CSES - Aalto Competitive Programming 2024 - wk9 - Wed - Results
Submission details
Task:Longest palindrome
Sender:aalto2024j_007
Submission time:2024-11-06 16:24:33 +0200
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#30.42 sdetails
#40.42 sdetails
#50.42 sdetails
#60.42 sdetails
#70.42 sdetails
#80.42 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#110.40 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#160.41 sdetails

Code

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

bool solve(vector<vector<bool>> &dp, int i, int j, string &s) {
    if (i == j) {
        return dp[i][j] = true;
    }
    if (j - i == 1) {
        if (s[i] == s[j]) {
            return dp[i][j] = true;
        } else {
            return dp[i][j] = false;
        }
    }
    if (s[i] == s[j] && dp[i + 1][j - 1] == true) {
        return dp[i][j] = true;
    } else {
        return dp[i][j] = false;
    }
}

string longestPalindrome(string s) {
    int n = s.size();
    int startIndex = 0;
    int maxlen = 0;
    vector<vector<bool>> dp(n, vector<bool>(n, false));
    for (int g = 0; g < n; g++) {
        for (int i = 0, j = g; j < n; i++, j++) {
            solve(dp, i, j, s);
            if (dp[i][j] == true) {
                if (j - i + 1 > maxlen) {
                    startIndex = i;
                    maxlen = j - i + 1;
                }
            }
        }
    }
    return s.substr(startIndex, maxlen);
}

int main() {
    string s;
    cin >> s;
    cout << longestPalindrome(s) << endl;
}

Test details

Test 1

Verdict: ACCEPTED

input
aaaaaaaaaa

correct output
aaaaaaaaaa

user output
aaaaaaaaaa

Test 2

Verdict: ACCEPTED

input
ababababab

correct output
ababababa

user output
ababababa

Test 3

Verdict:

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

user output
(empty)

Test 4

Verdict:

input
ababababababababababababababab...

correct output
ababababababababababababababab...

user output
(empty)

Test 5

Verdict:

input
aztdvxzjwxtrludymvpradgbdpgnrq...

correct output
aztdvxzjwxtrludymvpradgbdpgnrq...

user output
(empty)

Test 6

Verdict:

input
vvfigwwsyxbukedgcfyibvtbclybud...

correct output
vvfigwwsyxbukedgcfyibvtbclybud...

user output
(empty)

Test 7

Verdict:

input
abaabaaaaaaabaababbbbbbabaaabb...

correct output
babbbabbbaabbbbaabbbbbbbbaabbb...

user output
(empty)

Test 8

Verdict:

input
txolestmgyepwrpofxyesjtsfkhjac...

correct output
yxnbaabnxy

user output
(empty)

Test 9

Verdict: ACCEPTED

input
ihpohpzoffel

correct output
ff

user output
ff

Test 10

Verdict: ACCEPTED

input
flexflexvpqxierullgcfckjqflexf...

correct output
cfc

user output
cfc

Test 11

Verdict:

input
aabbabaabbaababbabaababbaabbab...

correct output
abaababbaabbabaababbabaabbaaba...

user output
(empty)

Test 12

Verdict: ACCEPTED

input
obsession

correct output
ses

user output
ses

Test 13

Verdict: ACCEPTED

input
abcxcbaxcba

correct output
abcxcba

user output
abcxcba

Test 14

Verdict: ACCEPTED

input
zzabc

correct output
zz

user output
zz

Test 15

Verdict: ACCEPTED

input
aaccaabbaaccaaccaabbaaccaa

correct output
aaccaabbaaccaaccaabbaaccaa

user output
aaccaabbaaccaaccaabbaaccaa

Test 16

Verdict:

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

user output
(empty)