CSES - Aalto Competitive Programming 2024 - wk9 - Wed - Results
Submission details
Task:Longest palindrome
Sender:aalto2024j_002
Submission time:2024-11-06 16:50:18 +0200
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3--details
#4--details
#5--details
#6--details
#7--details
#8--details
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11--details
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16--details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:70:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for(int i=0; i< s.size(); i++){
      |                  ~^~~~~~~~~~
input/code.cpp:88:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |         for(int i=0; i<sol.size(); i++){
      |                      ~^~~~~~~~~~~
input/code.cpp:98:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |         for(int i=0; i<sol.size(); i++){
      |                      ~^~~~~~~~~~~

Code

#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
 
pair<int,vector<char>> palind(string s, int i){

    vector<char> sol;

    int n = s.size();
    if(i==0){
        if(n>=2 and s[0]==s[1]){
            
            return make_pair(2,sol);
        }
        return make_pair(1,sol);
    }
    if(i==n-1)return make_pair(1,sol);
 
    int max1=1;
 
    int idx0=1;

    vector<char> sol1;
 
    while((i+idx0)<n and (i-idx0)>=0 and s[(i+idx0)]==s[(i-idx0)]){
        sol1.push_back(s[(i+idx0)]);
        max1 += 2;
        idx0 += 1;

        
    }
 
    int max2=1;

    vector<char> sol2;
 
    if(i+1 < n and s[i+1]==s[i]){
        max2 = 2;
        int idx1 = 1;
 
        while((i+idx1+1)<n and (i-idx1)>=0 and s[(i+idx1+1)]==s[(i-idx1)]){
            sol2.push_back(s[(i+idx1+1)]);
            max2 += 2;
            idx1 += 1;
            

        }
 
    }
 
    if(max2 > max1) return make_pair(max2,sol2);
    return make_pair(max1,sol1);
 
}


 
int main(){
    string s;
    cin >> s;
 
    int total = 1;

    int position = 0;

    vector<char> sol;
 
    for(int i=0; i< s.size(); i++){
        //int a = palind(s,i);
        pair<int,vector<char>> temp = palind(s,i);
        if(temp.first>total){
            total = temp.first;
            position = i;
            sol = temp.second;

        } 
    }

    //cout << total << endl;

    if(total%2==0){
        for(int i=sol.size()-1; i>=0; i--){
            cout<<sol[i];
        }
        cout << s[position] << s[position];
        for(int i=0; i<sol.size(); i++){
            cout << sol[i];
        }
        cout << endl;
    }
    else{
         for(int i=sol.size()-1; i>=0; i--){
            cout<<sol[i];
        }
        cout << s[position];
        for(int i=0; i<sol.size(); i++){
            cout << sol[i];
        }
        cout << 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)