Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <time.h>
- void solve(char * s, long long n, FILE *output) {
- char * p1 = NULL, * p2 = s + n;
- if (n == 1) {
- p1 = s;
- }
- for (long long i = n - 1; i >= 1; --i) {
- if (s[i - 1] < s[i]) {
- p1 = s + i - 1;
- } else {
- if (p1 != NULL) {
- break;
- }
- p2 = s + i;
- }
- }
- if (p1 != NULL) {
- long long i = 0;
- if (output == stdin) {
- while (p1 + i != p2) {
- printf("%c", *(p1 + i));
- i++;
- }
- } else {
- while (p1 + i != p2) {
- fprintf(stdin, "%c", *(p1 + i));
- i++;
- }
- }
- }
- }
- void generate_string(char * s, long long n) {
- for (int i = 0; i < n; ++i) {
- char tmp = rand() % 127 + 1;
- s[i] = tmp;
- }
- }
- int main(int argc, char **argv) {
- char * s;
- long long n;
- if (strcmp(argv[1], "-r") == 0) {
- srand(time(0));
- n = rand() % 2001;
- s = (char *)malloc((n + 1) * sizeof(char));
- generate_string(s, n);
- clock_t begin = clock();
- solve(s, n, stdin);
- clock_t end = clock();
- double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- printf("\nProgram execution time: %lf\n", time_spent);
- } else if (strcmp(argv[1], "-c") == 0) {
- scanf("%lld", &n);
- getchar();
- s = (char *)malloc((n + 1) * sizeof(char));
- fgets(s, n + 1, stdin);
- clock_t begin = clock();
- solve(s, n, stdin);
- clock_t end = clock();
- double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- printf("Program execution time: %lf", time_spent);
- } else if (strcmp(argv[1], "-f") == 0) {
- FILE *input, *output;
- input = fopen(argv[2], "r");
- output = fopen(argv[3], "w");
- fscanf(input, "%lld", &n);
- s = (char *)malloc((n + 1) * sizeof(char));
- fgetc(input);
- fgets(s, n + 1, input);
- clock_t begin = clock();
- solve(s, n, output);
- clock_t end = clock();
- double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- printf("Program execution time: %lf", time_spent);
- fclose(input);
- fclose(output);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment