Advertisement
_takumi

idz2_7

Nov 3rd, 2022
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. void solve(char * s, long long n, FILE *output) {
  5.     char * p1 = NULL, * p2 = s + n;
  6.     if (n == 1) {
  7.         p1 = s;
  8.     }
  9.     for (long long i = n - 1; i >= 1; --i) {
  10.         if (s[i - 1] < s[i]) {
  11.             p1 = s + i - 1;
  12.         } else {
  13.             if (p1 != NULL) {
  14.                 break;
  15.             }
  16.             p2 = s + i;
  17.         }
  18.     }
  19.     if (p1 != NULL) {
  20.         long long i = 0;
  21.         while (p1 + i != p2) {
  22.             fprintf(output, "%c", *(p1 + i));
  23.             i++;
  24.         }
  25.     }
  26. }
  27.  
  28. int main(int argc, char **argv) {
  29.     FILE *input, *output;
  30.     input = fopen(argv[1], "r");
  31.     output = fopen(argv[2], "w");
  32.     long long n = 0;
  33.     fscanf(input, "%lld", &n);
  34.     char * s = (char *)malloc((n + 1) * sizeof(char));
  35.     fgetc(input);
  36.     fgets(s, n + 1, input);
  37.     solve(s, n, output);
  38.     fclose(input);
  39.     fclose(output);
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement