CSES - E4590 2016 1 - Results
Submission details
Task:Reversals
Sender:EduardoVaca
Submission time:2016-09-17 14:47:28 +0300
Language:C++
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.06 sdetails
#2--details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:23:40: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i %i", &word_length, &queries);
                                        ^
input/code.cpp:27:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", word);
                   ^
input/code.cpp:32:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i %i", &start, &end);
                               ^

Code

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void swap(char *s, char *p){
	*s += *p;
	*p = *s - *p;
	*s = *s - *p;
}

void reverse_substring(char* start, char* end, int repetitions) {
	int i;
	for(i = 0; i < repetitions; i++) {
		swap(start++, end--);
	}
}

int main()
{
	int word_length, queries, i, start, end;
	char* word;

	scanf("%i %i", &word_length, &queries);

	word = (char*)malloc(word_length);

	scanf("%s", word);

	int rep;

	for(i = 0; i < queries; i++){
		scanf("%i %i", &start, &end);
		start--; end--;
		rep = abs(start - end);
		if (rep % 2 != 0)
			rep++;
		reverse_substring(&(word[start]), &(word[end]), rep / 2);		
	}
	printf("%s\n",word);

	free(word);
	
	return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
50 100
pplcmurzajsxlqqcrxewfhzqyihkzp...

correct output
fpuwlmatkzbhksppmjxpwurcvsdxcz...

user output
fpuwlmatkzbhksppmjxpwurcvsdxcz...

Test 2

Verdict:

input
500000 100000
slsmyuezdrenskmgkwxpcfzistssmu...

correct output
slsmyuezvdfzhssyoofpsnsagrrzri...

user output
(empty)