You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C++14提交SPOJ PALIN题目时遇Runtime error (SIGXFSZ)求助

Problem Analysis & Fix for SIGXFSZ Error in SPOJ PALIN

Key Issues in Original Code

  • Infinite Loop Causing Excessive Output (SIGXFSZ): When the code encounters a number >=1e6 or <=0, it increments testCase, which cancels the testCase-- in the outer loop condition. This leads to an infinite loop, repeatedly printing error messages until the output exceeds the platform's size limit, triggering the SIGXFSZ signal.
  • Invalid Input Type: Using int to store numbers up to 1e18 is incorrect—int can only hold values up to ~2e9 (32-bit). Large inputs cause overflow, leading to undefined behavior (e.g., negative numbers that trigger the error check and infinite loop).
  • Inefficient Brute-force Approach: Incrementing each number and checking for palindromes is too slow for large values (like 999...999), which would cause timeouts even if the runtime error is fixed.
  • Unnecessary Error Check: The problem guarantees valid positive integers up to 18 digits, so checking for numbers >=1e6 is incorrect and redundant.

Corrected Code

#include <iostream>
#include <string>
#include <algorithm>

std::string incrementString(std::string num) {
    int n = num.size();
    int carry = 1;
    for (int i = n - 1; i >= 0 && carry; --i) {
        int digit = num[i] - '0';
        digit += carry;
        carry = digit / 10;
        digit %= 10;
        num[i] = digit + '0';
    }
    if (carry) {
        num.insert(num.begin(), '1');
    }
    return num;
}

std::string nextPalindrome(std::string s) {
    int n = s.size();
    bool allNine = true;
    for (char c : s) {
        if (c != '9') {
            allNine = false;
            break;
        }
    }
    if (allNine) {
        return "1" + std::string(n - 1, '0') + "1";
    }
    
    std::string left = s.substr(0, (n + 1) / 2);
    std::string candidate = left;
    for (int i = left.size() - 1 - (n % 2); i >= 0; --i) {
        candidate += left[i];
    }
    
    if (candidate > s) {
        return candidate;
    } else {
        std::string newLeft = incrementString(left);
        std::string result = newLeft;
        for (int i = newLeft.size() - 1 - (n % 2); i >= 0; --i) {
            result += newLeft[i];
        }
        return result;
    }
}

int main() {
    int testCase;
    std::cin >> testCase;
    while (testCase--) {
        std::string num;
        std::cin >> num;
        std::cout << nextPalindrome(num) << std::endl;
    }
    return 0;
}

Fix Details

  1. Removed Redundant Error Check: Eliminates the infinite loop caused by testCase++.
  2. String-Based Handling: Supports 18-digit numbers without overflow by manipulating digits directly as strings.
  3. Efficient Palindrome Generation:
    • Checks if all digits are 9 (returns 1 followed by n-1 zeros and 1).
    • Creates a candidate palindrome by mirroring the left half of the number.
    • If the candidate is larger than the input, returns it; otherwise, increments the left half and mirrors to form the next valid palindrome.
  4. Edge Case Handling: Properly handles all-9 numbers and odd/even length inputs.

内容的提问来源于stack exchange,提问作者Log

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 00:48:12