Guest User

Untitled

a guest
Nov 21st, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. int cache[35][35];
  4.  
  5. void initialized_cache(){
  6. for (int i = 0; i < 35; i++){
  7. for (int j = 0; j < 35; j++){
  8. cache[i][j] = -1;
  9. }
  10. }
  11. }
  12.  
  13. int is_valid_char(char c) {
  14. if (c >= 'A' && c <= 'Z') return 1;
  15. if (c >= 'a' && c <= 'z') return 1;
  16. if (c >= '0' && c <= '9') return 1;
  17.  
  18. char valid_specials[] = " ',.~\\<>/?!@#$%^&*()_+ {}[]:;|";
  19. for (int i = 0; valid_specials[i] != '\0'; i++) {
  20. if (c == valid_specials[i]) return 1;
  21. }
  22.  
  23. return 0;
  24. }
  25.  
  26. void input_string(char *string, int *len_string){
  27. char char_input;
  28. for (int i = 0; i < 34; i++){
  29. if ((char_input = getchar()) == '\n' || char_input == EOF){
  30. string[i] = '\0';
  31. return;
  32. }
  33. if (!is_valid_char(char_input)) {
  34. printf("incorrect msg");
  35. string[i] = '\0';
  36. return;
  37. }
  38. string[i] = char_input;
  39. (*len_string)++;
  40. }
  41. }
  42.  
  43. int search_max_len_polindromic(char *string, int start, int end){
  44. if (start >= end) {
  45. if (start == end) {
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. if (cache[start][end] != -1){
  51. return cache[start][end];
  52. }
  53. if (string[start] == string[end]){
  54. cache[start][end] = 2 + search_max_len_polindromic(string, start + 1, end - 1);
  55. }
  56. else{
  57. int right = search_max_len_polindromic(string, start, end - 1);
  58. int left = search_max_len_polindromic(string, start + 1, end);
  59. cache[start][end] = (right > left) ? right : left;
  60. }
  61. return cache[start][end];
  62. }
  63.  
  64. void restore_polindromic(char *string, int start, int end, char *result, int *right, int *left, int *used){
  65. if (start > end){
  66. return;
  67. }
  68. if (start == end){
  69. result[*left] = string[start];
  70. used[start] = 1;
  71. return;
  72. }
  73. if (string[start] == string[end]){
  74. result[*left] = string[start];
  75. result[*right] = string[end];
  76. used[start] = 1;
  77. used[end] = 1;
  78. (*left)++;
  79. (*right)--;
  80. restore_polindromic(string, start + 1, end - 1, result, right, left, used);
  81. }
  82. else if (cache[start + 1][end] > cache[start][end - 1]){;
  83. restore_polindromic(string, start + 1, end - 1, result, right, left, used);
  84. }
  85. else{
  86. restore_polindromic(string, start, end - 1, result, right, left, used);
  87. }
  88. }
  89.  
  90. int main(){
  91. char string[35];
  92. int len_string = 0;
  93. char result[35] = {0};
  94. int used[35] = {0};
  95. input_string(string, &len_string);
  96. initialized_cache();
  97. search_max_len_polindromic(string, 0, len_string - 1);
  98. int left = 0;
  99. int right = cache[0][len_string - 1] - 1;
  100. restore_polindromic(string, 0, len_string - 1, result, &right, &left, used);
  101. result[len_string] = '\0';
  102. used[len_string] = '\0';
  103. printf("%s\n", result);
  104. for (int i = 0; i < len_string; i++){
  105. if (!used[i]){
  106. printf("%d - %c, ", i + 1, string[i]);
  107. }
  108. }
  109. return 0;
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment