1040. Longest Symmetric String (25)
Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given "Is PAT&TAP symmetric?", the longest symmetric sub-string is "s PAT&TAP s", hence you must output 11.
Input Specification:
Each input file contains one test case which gives a non-empty string of length no more than 1000.
Output Specification:
For each test case, simply print the maximum length in a line.
Sample Input:Is PAT&TAP symmetric?Sample Output:
11
#include <stdio.h>#include <string.h>#include <string>#include <iostream>#pragma warning(disable:4996)using namespace std;char a[1010];bool CheckSym(int i, int j) {for (int m = i; m <= (i + j) / 2; m++) {if (a[m] != a[i + j - m])return false;}return true;}int main(void) {int count = 0;int symMax = 1;while (true){scanf("%c", &a[count]);if (a[count] == '\n')break;count++;}for (int i = 0; i < count; i++) {if (count - i < symMax)break;for (int j = count; j > i; j--) {if (a[i] != a[j])continue;if (CheckSym(i, j) == true)if (j - i + 1>symMax)symMax = j - i + 1;}}cout << symMax;return 0;}
浙公网安备 33010602011771号