notc

Untitled

Oct 3rd, 2025
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <assert.h>
  6.  
  7. // Structure to store substring information - MUST BE DEFINED BEFORE USE
  8. typedef struct {
  9.     int start;
  10.     int end;
  11. } Substring;
  12.  
  13. // Helper function to check if string comparison shows s1 > s2
  14. bool isGreater(char* s1, char* s2) {
  15.     return strcmp(s1, s2) > 0;
  16. }
  17.  
  18. // Check if a string is a good binary string
  19. bool isGood(char* str, int len) {
  20.     int balance = 0;
  21.     for (int i = 0; i < len; i++) {
  22.         if (str[i] == '1') balance++;
  23.         else balance--;
  24.        
  25.         if (balance < 0) return false;
  26.     }
  27.     return balance == 0;
  28. }
  29.  
  30. char* largestMagical(char* binString) {
  31.     int len = strlen(binString);
  32.    
  33.     // Allocate result string
  34.     char* result = (char*)malloc((len + 1) * sizeof(char));
  35.     strcpy(result, binString);
  36.     result[len] = '\0';
  37.    
  38.     // Find all maximal good substrings
  39.     Substring* substrings = (Substring*)malloc(len * sizeof(Substring));
  40.     int substringCount = 0;
  41.    
  42.     int i = 0;
  43.     while (i < len) {
  44.         int balance = 0;
  45.         int start = i;
  46.         int j = i;
  47.        
  48.         while (j < len) {
  49.             if (result[j] == '1') balance++;
  50.             else balance--;
  51.            
  52.             if (balance < 0) break;
  53.            
  54.             if (balance == 0) {
  55.                 // Found a complete good substring
  56.                 substrings[substringCount].start = start;
  57.                 substrings[substringCount].end = j;
  58.                 substringCount++;
  59.                 i = j + 1;
  60.                 break;
  61.             }
  62.             j++;
  63.         }
  64.        
  65.         if (j >= len) break;
  66.     }
  67.    
  68.     // Perform bubble sort style swapping
  69.     bool swapped = true;
  70.     while (swapped) {
  71.         swapped = false;
  72.        
  73.         for (int idx = 0; idx < substringCount - 1; idx++) {
  74.             int start1 = substrings[idx].start;
  75.             int end1 = substrings[idx].end;
  76.             int start2 = substrings[idx + 1].start;
  77.             int end2 = substrings[idx + 1].end;
  78.            
  79.             // Check if they are adjacent
  80.             if (end1 + 1 != start2) continue;
  81.            
  82.             int len1 = end1 - start1 + 1;
  83.             int len2 = end2 - start2 + 1;
  84.            
  85.             // Create temporary strings for comparison
  86.             char* substr1 = (char*)malloc((len1 + 1) * sizeof(char));
  87.             char* substr2 = (char*)malloc((len2 + 1) * sizeof(char));
  88.            
  89.             strncpy(substr1, result + start1, len1);
  90.             substr1[len1] = '\0';
  91.             strncpy(substr2, result + start2, len2);
  92.             substr2[len2] = '\0';
  93.            
  94.             // Create swapped version
  95.             char* temp = (char*)malloc((len + 1) * sizeof(char));
  96.             strcpy(temp, result);
  97.            
  98.             // Swap the substrings
  99.             strncpy(temp + start1, substr2, len2);
  100.             strncpy(temp + start1 + len2, substr1, len1);
  101.            
  102.             // Check if swapped version is good and greater
  103.             if (isGood(temp, len) && isGreater(temp, result)) {
  104.                 strcpy(result, temp);
  105.                
  106.                 // Update substring positions
  107.                 substrings[idx].start = start1;
  108.                 substrings[idx].end = start1 + len2 - 1;
  109.                 substrings[idx + 1].start = start1 + len2;
  110.                 substrings[idx + 1].end = start1 + len2 + len1 - 1;
  111.                
  112.                 swapped = true;
  113.             }
  114.            
  115.             free(substr1);
  116.             free(substr2);
  117.             free(temp);
  118.         }
  119.     }
  120.    
  121.     free(substrings);
  122.     return result;
  123. }
  124.  
  125. char* readline() {
  126.     size_t alloc_length = 1024;
  127.     size_t data_length = 0;
  128.     char* data = malloc(alloc_length);
  129.    
  130.     while (true) {
  131.         char* cursor = data + data_length;
  132.         char* line = fgets(cursor, alloc_length - data_length, stdin);
  133.        
  134.         if (!line) {
  135.             break;
  136.         }
  137.        
  138.         data_length += strlen(cursor);
  139.        
  140.         if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
  141.             break;
  142.         }
  143.        
  144.         alloc_length <<= 1;
  145.         data = realloc(data, alloc_length);
  146.        
  147.         if (!data) {
  148.             data = '\0';
  149.             break;
  150.         }
  151.     }
  152.    
  153.     if (data[data_length - 1] == '\n') {
  154.         data[data_length - 1] = '\0';
  155.         data = realloc(data, data_length);
  156.        
  157.         if (!data) {
  158.             data = '\0';
  159.         }
  160.     } else {
  161.         data = realloc(data, data_length + 1);
  162.        
  163.         if (!data) {
  164.             data = '\0';
  165.         } else {
  166.             data[data_length] = '\0';
  167.         }
  168.     }
  169.    
  170.     return data;
  171. }
  172.  
  173. int main() {
  174.     FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
  175.    
  176.     char* binString = readline();
  177.     char* result = largestMagical(binString);
  178.    
  179.     fprintf(fptr, "%s\n", result);
  180.    
  181.     fclose(fptr);
  182.     free(binString);
  183.     free(result);
  184.    
  185.     return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment