fork download
  1.  
  2. //
  3. // palin5.c
  4. // Spoj.pl
  5. //
  6. // Created by Subodh Kamble on 17/04/13.
  7. // Copyright (c) 2013 Subodh Kamble. All rights reserved.
  8. //
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. void copyRemainingElements(char *,char * , int , int);
  15. int compareElements(char *,char *, int , int);
  16. void merge(char *, char *);
  17.  
  18. int main()
  19. {
  20. int t ;
  21. char *input , *output , *first , *second;
  22. int i , startCopyingLocation , l ;
  23. scanf("%d",&t);
  24.  
  25. while (t--)
  26. {
  27. input = (char *)malloc(1000000);
  28. scanf("%s",input);
  29. l = strlen(input);
  30.  
  31. i = 0 ;
  32. for(i = 0 ; i< l ; i++)
  33. {
  34. if(input[i]!='9')
  35. break;
  36. }
  37.  
  38. if(i==l)
  39. {
  40. output = malloc(l+2);
  41. output[0]='1';
  42. output[l]='1';
  43. for(i=1;i<l;i++)
  44. output[i]='0';
  45. output[l+1]='\0';
  46. printf("%s\n",output);
  47. break;
  48. }
  49.  
  50. if (l%2==0)
  51. {
  52. //even
  53. first = malloc(l/2+1);
  54. second = malloc(l/2+1);
  55.  
  56. for(i=0;i<l/2;i++)
  57. first[i] = input[l/2-1-i];
  58. first[i]='\0';
  59.  
  60. for(i=l/2;i<l;i++)
  61. second[i-l/2]=input[i];
  62.  
  63. i = 0 ;
  64. startCopyingLocation = compareElements(first,second,i,i);
  65. copyRemainingElements(first,second,startCopyingLocation,startCopyingLocation);
  66. merge(first,second);
  67.  
  68. }
  69. else
  70. {
  71.  
  72. }
  73. }
  74.  
  75. free(input);
  76. free(output);
  77.  
  78. return 0 ;
  79. }
  80. void copyRemainingElements(char *first,char *second , int startCopyingLocation1, int startCopyingLocation2)
  81. {
  82. if(startCopyingLocation1 == -1)
  83. startCopyingLocation1 = 0 ;
  84. while(first[startCopyingLocation1] !='\0')
  85. {
  86. second[startCopyingLocation1] = first[startCopyingLocation1] ;
  87. startCopyingLocation1++;
  88. }
  89.  
  90. }
  91. int compareElements(char *first,char *second,int iterateFirst, int iterateSecond )
  92. {
  93. int startCopyingLocation = 0 ;
  94. int noOfNine = 0 ;
  95. int containsNine = 0 ;
  96.  
  97. if(first[0]=='9')
  98. containsNine = 1 ;
  99. while(first[iterateFirst]!='\0')
  100. {
  101. if(first[iterateFirst]==second[iterateSecond])
  102. {
  103. if(containsNine==1 && first[iterateFirst]=='9')
  104. noOfNine = noOfNine + 1 ;
  105. else
  106. containsNine = 0 ;
  107. iterateFirst++;
  108.  
  109. }
  110. else if(first[iterateFirst] < second[iterateSecond])
  111. {
  112. //handle the case of 9
  113.  
  114. if(noOfNine != 0)
  115. {
  116. for(containsNine = 0;containsNine<noOfNine;containsNine++)
  117. {
  118. first[containsNine] = '0' ;
  119. }
  120.  
  121.  
  122. }
  123.  
  124. first[iterateFirst]=first[iterateFirst]+1;
  125. second[iterateSecond]=first[iterateFirst];
  126.  
  127. startCopyingLocation = iterateFirst+1 ;
  128. if(startCopyingLocation == strlen(first))
  129. return -1 ;
  130. else
  131. return startCopyingLocation ;
  132. }
  133. else if(first[iterateFirst] > second[iterateSecond])
  134. {
  135. startCopyingLocation = iterateFirst ;
  136. return startCopyingLocation ;
  137. }
  138. }
  139.  
  140. }
  141.  
  142. void merge(char *first,char *second)
  143. {
  144. char *output = malloc(strlen(first)+strlen(second));
  145. unsigned long long int i = 0 ;
  146. while(first[i]!='\0')
  147. {
  148. output[i]=first[strlen(first)-1-i];
  149. i++;
  150. }
  151. output[i]='\0';
  152. strcat(output,second);
  153. printf("%s\n",output);
  154. }
  155.  
  156.  
Success #stdin #stdout 0s 1924KB
stdin
Standard input is empty
stdout
1