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

来源:互联网 发布:飞飞cms安装教程 编辑:程序博客网 时间:2024/05/10 10:08

注:找不到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;}
0 0