notc

Untitled

Oct 3rd, 2025 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.99 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. // Helper function to check if string comparison shows s1 > s2
  8. bool isGreater(char* s1, char* s2) {
  9.     return strcmp(s1, s2) > 0;
  10. }
  11.  
  12. // Check if a string is a good binary string
  13. bool isGood(char* str, int len) {
  14.     int balance = 0;
  15.     for (int i = 0; i < len; i++) {
  16.         if (str[i] == '1') balance++;
  17.         else balance--;
  18.        
  19.         if (balance < 0) return false;
  20.     }
  21.     return balance == 0;
  22. }
  23.  
  24. // Structure to store substring information
  25. typedef struct {
  26.     int start;
  27.     int end;
  28. } Substring;
  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. int main() {
  126.     FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
  127.    
  128.     char* binString = readline();
  129.     char* result = largestMagical(binString);
  130.    
  131.     fprintf(fptr, "%s\n", result);
  132.    
  133.     fclose(fptr);
  134.     free(binString);
  135.     free(result);
  136.    
  137.     return 0;
  138. }
  139.  
  140. char* readline() {
  141.     size_t alloc_length = 1024;
  142.     size_t data_length = 0;
  143.     char* data = malloc(alloc_length);
  144.    
  145.     while (true) {
  146.         char* cursor = data + data_length;
  147.         char* line = fgets(cursor, alloc_length - data_length, stdin);
  148.        
  149.         if (!line) {
  150.             break;
  151.         }
  152.        
  153.         data_length += strlen(cursor);
  154.        
  155.         if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
  156.             break;
  157.         }
  158.        
  159.         alloc_length <<= 1;
  160.         data = realloc(data, alloc_length);
  161.        
  162.         if (!data) {
  163.             data = '\0';
  164.             break;
  165.         }
  166.     }
  167.    
  168.     if (data[data_length - 1] == '\n') {
  169.         data[data_length - 1] = '\0';
  170.         data = realloc(data, data_length);
  171.        
  172.         if (!data) {
  173.             data = '\0';
  174.         }
  175.     } else {
  176.         data = realloc(data, data_length + 1);
  177.        
  178.         if (!data) {
  179.             data = '\0';
  180.         } else {
  181.             data[data_length] = '\0';
  182.         }
  183.     }
  184.    
  185.     return data;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment