_takumi

idz2_8

Nov 6th, 2022 (edited)
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5.  
  6. void solve(char * s, long long n, FILE *output) {
  7.     char * p1 = NULL, * p2 = s + n;
  8.     if (n == 1) {
  9.         p1 = s;
  10.     }
  11.     for (long long i = n - 1; i >= 1; --i) {
  12.         if (s[i - 1] < s[i]) {
  13.             p1 = s + i - 1;
  14.         } else {
  15.             if (p1 != NULL) {
  16.                 break;
  17.             }
  18.             p2 = s + i;
  19.         }
  20.     }
  21.     if (p1 != NULL) {
  22.         long long i = 0;
  23.         if (output == stdin) {
  24.             while (p1 + i != p2) {
  25.                 printf("%c", *(p1 + i));
  26.                 i++;
  27.             }
  28.         } else {
  29.             while (p1 + i != p2) {
  30.                 fprintf(stdin, "%c", *(p1 + i));
  31.                 i++;
  32.             }
  33.         }
  34.  
  35.     }
  36. }
  37.  
  38. void generate_string(char * s, long long n) {
  39.     for (int i = 0; i < n; ++i) {
  40.         char tmp = rand() % 127 + 1;
  41.         s[i] = tmp;
  42.     }
  43. }
  44.  
  45. int main(int argc, char **argv) {
  46.     char * s;
  47.     long long n;
  48.     if (strcmp(argv[1], "-r") == 0) {
  49.         srand(time(0));
  50.         n = rand() % 2001;
  51.         s = (char *)malloc((n + 1) * sizeof(char));
  52.         generate_string(s, n);
  53.         clock_t begin = clock();
  54.         solve(s, n, stdin);
  55.         clock_t end = clock();
  56.         double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  57.         printf("\nProgram execution time: %lf\n", time_spent);
  58.     } else if (strcmp(argv[1], "-c") == 0) {
  59.         scanf("%lld", &n);
  60.         getchar();
  61.         s = (char *)malloc((n + 1) * sizeof(char));
  62.         fgets(s, n + 1, stdin);
  63.         clock_t begin = clock();
  64.         solve(s, n, stdin);
  65.         clock_t end = clock();
  66.         double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  67.         printf("Program execution time: %lf", time_spent);
  68.     } else if (strcmp(argv[1], "-f") == 0) {
  69.         FILE *input, *output;
  70.         input = fopen(argv[2], "r");
  71.         output = fopen(argv[3], "w");
  72.         fscanf(input, "%lld", &n);
  73.         s = (char *)malloc((n + 1) * sizeof(char));
  74.         fgetc(input);
  75.         fgets(s, n + 1, input);
  76.         clock_t begin = clock();
  77.         solve(s, n, output);
  78.         clock_t end = clock();
  79.         double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  80.         printf("Program execution time: %lf", time_spent);
  81.         fclose(input);
  82.         fclose(output);
  83.     }
  84.     return 0;
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment