Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #ifndef DEBUG
- #define DEBUG(...) printf(__VA_ARGS__)
- #endif
- void swap(char *a, char *b){
- char tmp = *a;
- *a = *b;
- *b = tmp;
- }
- void reverse(char *s, char *l, char *r) {
- int n = *r - *l;
- // zasto izbacuje negativan n kad reversam od 0 do kraja stringa ????
- printf("\n n = %d\n", n);
- int n2 = 0;
- while(*s){
- n2++;
- s++;
- }
- //printf("\n n2 = %d\n", n2);
- if (n2 < 4 || n == 1) // ako su 2 ili 3 elementa
- {
- swap(r,l);
- printf("\n--Usao sam u 1. if--\n");
- }
- if (n%2==0 && n != 2)
- {
- for (int i = 0; i <= n/2; ++i)
- {
- swap(l+i,r-i);
- printf("\n--Usao sam u 2. if--\n");
- }
- }else if (n%2==1 && n != 1)
- {
- for (int i = 0; i <= n/2; ++i)
- {
- swap(l+i,r-i);
- printf("\n--Usao sam u 3. if--\n");
- }
- }
- }
- int main() {
- int n, q;
- char *s;
- scanf("%d%d", &n, &q);
- s = malloc((n+1) * sizeof(char));
- scanf(" %s", s);
- for (int i = 0; i < q; ++i) {
- int l, r;
- scanf("%d%d", &l, &r);
- reverse(s, s+l, s+r);
- }
- printf("%s\n", s);
- free(s);
- return 0;
- }
- /*
- // nesluzbeni testcase koji zadaje muke
- 4 1
- dcba
- 0 3
- n = -3 ovdje zasto ??????
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement