fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. char* input = "FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth";
  7.  
  8. char* sub_seq(const char* source, int start, int end)
  9. {
  10. char* result;
  11. int i;
  12. result = malloc((end - start + 1) * sizeof(char));
  13. for (i = 0; i < end - start; ++i)
  14. {
  15. result[i] = source[i + start];
  16. }
  17. result[end - start] = '\0';
  18. return result;
  19. }
  20.  
  21. char* seek_palindrome(const char* input)
  22. {
  23. char* answer;
  24. answer = malloc(sizeof(char));
  25. answer[0] = '\0';
  26.  
  27. int len, i, j, k;
  28. len = strlen(input);
  29.  
  30. for (i = 1; i < len - 1; ++i)
  31. {
  32. for (j = i - 1, k = i + 1; j >= 0 && k < len; --j, ++k)
  33. {
  34. if (input[j] == input[k])
  35. {
  36. char* palindrome = sub_seq(input, j, k + 1);
  37. if (strlen(palindrome) > strlen(answer))
  38. {
  39. free(answer); answer = NULL;
  40. answer = palindrome;
  41. }
  42. else
  43. {
  44. free(palindrome);
  45. }
  46. }
  47. else
  48. {
  49. break;
  50. }
  51. }
  52. }
  53.  
  54. for (i = 1; i < len - 1; ++i)
  55. {
  56. for (j = i, k = i + 1; j >= 0 && k < len; --j, ++k)
  57. {
  58. if (input[j] == input[k])
  59. {
  60. char* palindrome = sub_seq(input, j, k + 1);
  61. if (strlen(palindrome) > strlen(answer))
  62. {
  63. free(answer); answer = NULL;
  64. answer = palindrome;
  65. }
  66. else
  67. {
  68. free(palindrome);
  69. }
  70. }
  71. else
  72. {
  73. break;
  74. }
  75. }
  76. }
  77. return answer;
  78. }
  79.  
  80. int main(int argc, char* argv[])
  81. {
  82. clock_t t;
  83. char* result;
  84. t = clock();
  85. result = seek_palindrome(input);
  86. t = clock() - t;
  87.  
  88. printf("The longest palindrome is '%s' calculated in %f seconds\n", result, ((float) t) / CLOCKS_PER_SEC);
  89. free(result); result = NULL;
  90. return EXIT_SUCCESS;
  91. }
Success #stdin #stdout 0s 1964KB
stdin
Standard input is empty
stdout
The longest palindrome is 'ranynar' calculated in 0.000000 seconds