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

 

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <string>
  4. #include <iostream>
  5. #pragma warning(disable:4996)
  6. using namespace std;
  7. char a[1010];
  8. bool CheckSym(int i, int j) {
  9. for (int m = i; m <= (i + j) / 2; m++) {
  10. if (a[m] != a[i + j - m])
  11. return false;
  12. }
  13. return true;
  14. }
  15. int main(void) {
  16. int count = 0;
  17. int symMax = 1;
  18. while (true)
  19. {
  20. scanf("%c", &a[count]);
  21. if (a[count] == '\n')
  22. break;
  23. count++;
  24. }
  25. for (int i = 0; i < count; i++) {
  26. if (count - i < symMax)
  27. break;
  28. for (int j = count; j > i; j--) {
  29. if (a[i] != a[j])
  30. continue;
  31. if (CheckSym(i, j) == true)
  32. if (j - i + 1>symMax)
  33. symMax = j - i + 1;
  34. }
  35. }
  36. cout << symMax;
  37. return 0;
  38. }





posted @ 2015-12-06 10:46  白夜行zz  阅读(188)  评论(0)    收藏  举报