第四题最长回文子串WA了很久了谁来帮我看看code...TAT

0
0
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXL = 1e6 + 2;
int counter[2 * MAXL], kk;
char s[MAXL * 2];

int LongestPalindrome()
{
    int j = 0, _max = 0, ans = 0, len = kk;
    counter[0] = 1;
    for(int i = 1; i < len; i++){
        int st = 1, cnt = 1;
        if(2 * j >= i){
            int low = counter[2 * j - i] < (counter[j] - 2 * (i - j)) ? counter[2 * j - i] : (counter[j]  - 2 * (i - j));
            cnt = cnt > low ? cnt : low;
        }
        st = cnt / 2 + 1;
        for(int k = st; k <= i && k < len - i; k++)
        {
            if(s[i + k] == s[i - k])
                cnt += 2;
            else
                break;
        }
        counter[i] = cnt;
        ans = ans > cnt ? ans : cnt;
        if(i + counter[i] / 2 > _max){
            j = i;
            _max = i + counter[i] / 2;
        }
    }
    return ans / 2;
}

int main()
{
    int n;
    char ch;
    scanf("%d", &n);
    getchar();
    for(int i = 0; i < n; i++)
    {
        kk = 0;
        s[kk++] = '$';
        while((ch = getchar()) != '\n'){
            s[kk++] = ch;
            s[kk++] = '$';
        }
        s[kk++] = '\0';
        if(kk != 2)
            printf("%d\n", LongestPalindrome());
    }
    return 0;
}

1 answer(s)

0

不要用getchar,貌似测试数据中间会输入多个回车。我改成整行输入就过了。我也调了好久。。。。

write answer 切换为英文 切换为中文


转发分享