## 1177 - Group Reverse

Discussion around the problems of the COJ.
Forum rules
Remember that posting AC code is not allowed here. If you are going to ask a question or to post a solution, describe your algorithm instead. Posting AC code will be penalized.
dovier
Posts: 1143
Joined: 8 years ago
Location: Havana, Cuba
Gender:  ### 1177 - Group Reverse

smprogrammer
Gender: ### Re: 1177 - Group Reverse

Hello anyone!

I can't understand why my C implementation returns TLE! What basically my algorithm does, is to take the string, calculate the length of one group, and for each group existent in the given string, reverse its characters. This last task involves the use of a loop.

So, I think I get TLE for using TWO loops .

Is there a more efficient way to do this? I've tried not to reverse and just print the characters in the desired order, but still, I get TLE in the first test.

Thanks in advance! Dariel
Posts: 51
Joined: 8 years ago
Location: Santiago de Cuba
Gender: Contact:

### Re: 1177 - Group Reverse

My solution is made in C + + and uses two cycles, but what I do is go from group to group. For example:
I have the string S = "ABCEHSHSH" and G = 3

Code: Select all

``````L = | S | / / size of S -> | S | = 9
C = L / G
K = 0
FOR i =C - 1 TO L WITH JUMP  C DO
BEGIN
FOR j= i DOWNTO K WITH JUMP -1  DO
PRINT S[j]
K = K+C
END
``````
This is the fundamental idea.

smprogrammer
Gender: ### Re: 1177 - Group Reverse

Dariel wrote:My solution is made in C + + and uses two cycles, but what I do is go from group to group. For example:
I have the string S = "ABCEHSHSH" and G = 3

Code: Select all

``````L = | S | / / size of S -> | S | = 9
C = L / G
K = 0
FOR i =C - 1 TO L WITH JUMP  C DO
BEGIN
FOR j= i DOWNTO K WITH JUMP -1  DO
PRINT S[j]
K = K+C
END
``````
This is the fundamental idea.
Thanks, Dariel. I've addressed the problem with your proposal and it worked! Got AC.

I think it's for C++. I've had so many problems with C strings I/O at the point two essentially identical solutions give different results! One written in C (using char*, fgets, setbuf...) gets WA whereas the other in C++ (using strings) gets AC.

Thanks, anyway! 