【PAT甲级】1040. Longest Symmetric String (25)

时间:2020-12-30 21:56:09

注:找不到Symmetric String时,打印长度为1。

#include <stdio.h>
#include <string.h>
#include <string>

int main(int argc, char* argv[]) {
    char a[1001];
    gets(a);
    int n = strlen(a);

    int h, e;
    int lh, le;
    int maxlen = 1;
    int len = 0;
    int i, j;
    for (i = 0; i < n; i++) {
        j = n - 1; 
        while (a[j] != a[i] && j > i) j--;
        while (j > i) {
            le = e = j;
            lh = h = i;
            while (h < e && a[h] == a[e]) {
                h++;
                e--;
            }
            if (h == e || h > e) {
                len = le - lh + 1;
                if (maxlen < len)
                    maxlen = len;
                j--;
                break;
            } else {
                j--;
                while (a[j] != a[i] && j > i) j--;
            }
        }
    }
    printf("%d\n", maxlen);
    return 0;
}