-
-
Save garganshul108/b82e137099c1db7726d044e95f802800 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////https://www.spoj.com/problems/JULKA/ | |
/* | |
* | |
* 1. strings ko endpoint ni mil rha tha | |
* explicitly add '\0' at end of each char array like 112 => 112\0 | |
* 2. index out of bound possible | |
* garbage return hora tha | |
* | |
* | |
*/ | |
#include <iostream> | |
#include <cstring> | |
#define debug(p) cout<<#p<<' '<<p<<'\n'; | |
#define fr(v,s,n) for(int v=s;((s<n)?v<n:v>n);((s<n)?v++:v--)) | |
#define cstop cin>>stop; | |
using namespace std; | |
char stop; | |
int main() | |
{ | |
int t=10; | |
int size = 105; | |
while (t--) | |
{ | |
char app_together[size], app_more[size], app_together_untouched[size]; | |
cin>>app_together; | |
cin>>app_more; | |
for(int i=0; i<size; i++) | |
{ | |
app_together_untouched[i] = app_together[i]; | |
} | |
int len_app_together = strlen(app_together); | |
int len_app_more = strlen(app_more); | |
char ans[size]; | |
ans[len_app_together] = '\0'; // IMPORTANT : 1 | |
int flag = 1; | |
for (int i=len_app_together-1; i>=0; i--) | |
{ | |
if (flag) | |
{ | |
for (int j=len_app_more-1; j>=0; j--) | |
{ | |
char a = app_together[i]; | |
char b = app_more[j]; | |
if(a >= b) { ans[i] = 48 + a - b; } | |
if(a < b) | |
{ | |
ans[i] = 48 + (10 + a - b); | |
app_together[i-1] -= 1; | |
} | |
i--; | |
} | |
} | |
flag = 0; | |
/* CAUTION: index can get out of bound here when i = -1 */ | |
if (i > -1 && (int)app_together[i] + 1 == 48) // IMPORTANT : 2 | |
{ | |
ans[i] = app_together[i] + 10; | |
app_together[i-1] -= 1; | |
} | |
if (i > -1 && (int)app_together[i] >= 48) // IMPORTANT : 2 | |
{ | |
ans[i] = app_together[i]; | |
} | |
} | |
int len_ans = strlen(ans); | |
char final_ans[size]; | |
final_ans[len_ans] = '\0'; // IMPORTANT : 1 | |
int next_step_add = 0; | |
for (int i=0 ; i < len_ans; i++) | |
{ | |
int x = ans[i]; | |
x = x-48+next_step_add; | |
if(x>=2) | |
{ | |
if (x%2 == 0) | |
{ | |
final_ans[i] = x/2 + 48; | |
next_step_add = 0; | |
} | |
else | |
{ | |
final_ans[i] = x/2 + 48; | |
next_step_add=10; | |
continue; | |
} | |
} | |
else | |
{ | |
final_ans[i] = x/2 + 48; | |
if (x%2 == 0) | |
{ | |
next_step_add = 0; | |
} | |
else | |
{ | |
next_step_add = 10; | |
} | |
} | |
} | |
int len_final_ans = strlen(final_ans); | |
char final_ans_mod[size]; | |
int flag_for_zeros_before_num = 1; | |
int counter =0; | |
for(int i=0; i<=len_final_ans; i++) | |
{ | |
if (final_ans[i] == '0' && flag_for_zeros_before_num) | |
{ | |
counter++; | |
continue; | |
} | |
else | |
{ | |
final_ans_mod[i-counter] = final_ans[i]; | |
flag_for_zeros_before_num = 0; | |
} | |
} | |
cout<<final_ans_mod<<"\n"; | |
char final_ans_klaudia[size]; | |
int len_final_ans_mod = strlen(final_ans_mod); | |
int len_app_together_untouched = strlen(app_together_untouched); | |
final_ans_klaudia[len_app_together_untouched] = '\0'; // IMPORTANT : 1 | |
flag = 1; | |
for (int i=len_app_together_untouched-1; i>=0; i--) | |
{ | |
if (flag) | |
{ | |
for (int j=len_final_ans_mod-1; j>=0; j--) | |
{ | |
char a = app_together_untouched[i]; | |
char b = final_ans_mod[j]; | |
if(a >= b) | |
{ | |
final_ans_klaudia[i] = 48 + a - b; | |
} | |
if(a < b) | |
{ | |
final_ans_klaudia[i] = 48 + (10 + a - b); | |
app_together_untouched[i-1] -= 1; | |
} | |
i--; | |
} | |
} | |
flag = 0; | |
if (i > -1 && (int)app_together_untouched[i] + 1 == 48) // IMPORTANT : 1 | |
{ | |
final_ans_klaudia[i] = app_together[i] + 10; | |
app_together_untouched[i-1] -= 1; | |
} | |
if (i > -1 && (int)app_together_untouched[i] >= 48) // IMPORTANT : 1 | |
{ | |
final_ans_klaudia[i] = app_together_untouched[i]; | |
} | |
} | |
int len_final_ans_klaudia = strlen(final_ans_klaudia); | |
flag_for_zeros_before_num = 1; | |
for(int i=0; i<len_final_ans_klaudia; i++) | |
{ | |
if (final_ans_klaudia[i] == '0' && flag_for_zeros_before_num) | |
{ | |
continue; | |
} | |
else | |
{ | |
flag_for_zeros_before_num = 0; | |
cout<<final_ans_klaudia[i]; | |
} | |
} | |
cout<<"\n"; | |
} | |
return 0; | |
} |
Even this has a minor mistake.
Try running this with input
100
2 .
I've corrected that in the final submission.
Here -> (https://gist.github.com/AnirudhDagar/6aac9a15e6072aa9eac5f8be57caf352)
Even this has a minor mistake.
Try running this with input
100
2 .
I've corrected that in the final submission.
Here -> (https://gist.github.com/AnirudhDagar/6aac9a15e6072aa9eac5f8be57caf352)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool! @garganshul108
I actually thought that the problem may be related to the null character.
And thanks for pointing out the case where the index went out of bounds.
I totally missed that.
Finally solved.
Thanks for debugging. :) 💯