/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 1ms 532.0 KiB
#2 Wrong Answer 1ms 532.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;

static const int MOD = 1000000007;

// A helper function to add two numbers modulo MOD
inline long long modAdd(long long a, long long b) {
    return (a + b) % MOD;
}

// Compute the "distance" between two even-length palindromes S and T
// in a direct manner.
//
// This direct function can be used for demonstration or for smaller subproblems.
// For large constraints, you'll need a more optimized approach.
long long palindromeDistance(const string &S, const string &T) {
    // S and T are guaranteed even-length palindromes.
    // We can simulate or derive a formula based on matching outer layers.
    
    // 1) Find longest matching outer "palindromic" layer from S and T.
    //    Because S and T are each palindromic, we compare from the outside in.
    int lenS = (int)S.size();
    int lenT = (int)T.size();
    
    // The maximum number of matching layers from each side
    // will be the largest k such that the first k/2 characters of S  = T
    // and the last k/2 characters of S = T, etc.
    // We'll do a quick approach here:
    int matches = 0;
    int i = 0, j = 0;
    // Expand inwards while characters match
    while (i < lenS && j < lenT && S[i] == T[j] && S[lenS - 1 - i] == T[lenT - 1 - j]) {
        i++; j++;
        matches += 2; // matched two characters from the outside: front and back
    }
    
    // matches now is how many total matching characters from the outside we have.
    // But the largest "layer" overlap is matches in terms of length.
    // The distance formula is typically:
    //   distance(S, T) = (|S| - matches)/2 + (|T| - matches)/2
    // because each unmatched outer pair must be shrunk away from S or T.
    long long dist = ( (lenS - matches) + (lenT - matches) ) / 2LL;
    
    return dist;
}

// Center expansion to find all even-length palindromes in O(N^2) worst-case.
// For large N, you should use Manacher’s algorithm or a more optimized method.
vector<pair<int,int>> findEvenLengthPalindromes(const string &P) {
    vector<pair<int,int>> result; // store (startIndex, endIndex), 0-based
    int n = (int)P.size();
    
    // For each gap between i and i+1, expand outward
    for (int center = 0; center < n - 1; center++) {
        int left = center, right = center + 1;
        while (left >= 0 && right < n && P[left] == P[right]) {
            // record this even palindrome [left, right]
            result.push_back({left, right});
            left--;
            right++;
        }
    }
    return result;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while(T--){
        string P;
        cin >> P;
        int n = (int)P.size();

        // 1) Gather all even-length palindromic substrings.
        auto palSubs = findEvenLengthPalindromes(P);

        // If there are M palindromic substrings, potentially we have M*(M-1)/2 pairs.
        // We do a direct sum for demonstration. For large N, you'll need an
        // approach more advanced than O(M^2).
        long long ans = 0LL;

        // Convert each pair (start, end) into the corresponding substring for distance check.
        // CAUTION: Doing this for large input is not feasible. This is for demonstration.
        for (int i = 0; i < (int)palSubs.size(); i++) {
            for (int j = i + 1; j < (int)palSubs.size(); j++) {
                // Extract the substrings
                auto [l1, r1] = palSubs[i];
                auto [l2, r2] = palSubs[j];
                
                string s1 = P.substr(l1, r1 - l1 + 1);
                string s2 = P.substr(l2, r2 - l2 + 1);
                
                // Compute distance
                long long d = palindromeDistance(s1, s2);
                ans = modAdd(ans, d);
            }
        }

        cout << ans % MOD << "\n";
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1144 Palindromic Distance
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-21 16:44:41
Judged At
2024-12-21 16:44:41
Judged By
Score
0
Total Time
1ms
Peak Memory
532.0 KiB