/ SeriousOJ /

Record Detail

Memory Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 1ms 540.0 KiB
#3 Memory Exceeded ≥280ms ≥256.016 MiB
#4 Time Exceeded ≥1101ms ≥31.578 MiB

Code

#include <bits/stdc++.h>

using namespace std;

bool is_palindrome(string s)
{
    int n = s.size();
    for (int i = 0; i < n / 2; i++)
    {
        if (s[i] != s[n - i - 1])
        {
            return false;
        }
    }
    return true;
}

int distance(string s, string t)
{
    if (s.size() < t.size())
    {
        swap(s, t);
    }
    int dist = 0;
    while (s.size() > t.size())
    {
        s.erase(s.begin());
        s.pop_back();
        dist++;
    }
    while (s.size() && s != t)
    {
        s.erase(s.begin());
        s.pop_back();
        t.erase(t.begin());
        t.pop_back();
        dist += 2;
    }
    return dist;
}

int main()
{
    int t = 0;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        int n = s.size();
        vector<string> palindromes;
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j+=2)
            {
                string sub = s.substr(i, j - i + 1);
                if (is_palindrome(sub))
                {
                    palindromes.push_back(sub);
                }
            }
        }

        // for (auto x: palindromes) cout << x << " "; cout << endl;
        int ans = 0;
        for (int i = 0; i < palindromes.size(); i++)
        {
            for (int j = i + 1; j < palindromes.size(); j++)
            {
                ans += distance(palindromes[i], palindromes[j]);
                // cout << palindromes[i] << " " << palindromes[j] << " " << distance(palindromes[i], palindromes[j]) << endl;
            }
        }
        cout << ans << endl;

    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1144 Palindromic Distance
Contest
LU Divisonal Contest Problem Testing Round
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-08 18:38:42
Judged At
2024-12-08 18:38:42
Judged By
Score
10
Total Time
≥1101ms
Peak Memory
≥256.016 MiB