fork download
  1. #include <iostream>
  2. using namespace std;
  3. #include <unordered_set>
  4. #include <bits/stdc++.h>
  5. #include <limits.h>
  6. #include <unordered_map>
  7. unordered_map<string, string> otherColors;
  8.  
  9. void processString(string& sameColor, int& total, char c){
  10. cout << sameColor.length() << endl;
  11. if(sameColor.length() == 0) return "";
  12. char first = sameColor[0];
  13. if(sameColor.length() == 1) return sameColor;
  14. if(sameColor.length() % 2 == 1){
  15. for(int i= 1; i < sameColor.length(); i+=2)
  16. sameColor[i] = otherColors[string(1,first)][0];
  17. }else{
  18. //if it is the even color
  19. if(sameColor.length() == 2){
  20. cout << "here";
  21. if(c != ' ')
  22. sameColor[1] = otherColors[string(1,sameColor[0]) + string(1,c)][0];
  23. else
  24. sameColor[1] = otherColors[string(1,sameColor[0])][0];
  25. } else{
  26. string s1 = sameColor.substr(0, sameColor.length()/2);
  27. string s2 = sameColor.substr(sameColor.length()/2);
  28. sameColor[s1.length()] = otherColors[string(1,first)][0];
  29. s1 = s1.substr(s1.length()-1);
  30. processString(s1, total, otherColors[string(1,first)][0]);
  31. processString(s2, total,' ');
  32. sameColor = s1+s2;
  33. }
  34. }
  35. }
  36. int main() {
  37. // your code goes here
  38. otherColors["B"] = "RG";
  39. otherColors["R"] = "BG";
  40. otherColors["G"] = "BR";
  41. otherColors["BR"] = "G";
  42. otherColors["RB"] = "G";
  43. otherColors["BG"] = "R";
  44. otherColors["GB"] = "R";
  45. otherColors["GR"] = "B";
  46. otherColors["RG"] = "B";
  47. int T;
  48. cin >> T;
  49. string colors, result;
  50. cin >> colors;
  51. int total =0, minChange = INT_MAX;
  52. char prev = ' ';
  53. //cout << "checking " << s << endl
  54. string sameColor = colors;
  55. string nextColor;
  56. for(int i=0; i<T;i++){
  57.  
  58. if(prev == ' ' || colors[i] == prev){
  59. nextColor += colors[i];
  60. }else{
  61. //prcoess the color
  62. cout << "processing " << nextColor <<endl;
  63. char c = i <colors.length() -1 ? colors[i] : ' ';
  64. processString(nextColor, total, c);
  65. cout <<" result " << nextColor << endl;
  66. nextColor ="";
  67. }
  68.  
  69. prev = colors[i];
  70.  
  71. }
  72. processString(sameColor, total, ' ');
  73. cout << total << endl;
  74. cout << sameColor << endl;
  75. return 0;
  76. }
Compilation error #stdin compilation error #stdout 0s 80768KB
stdin
13
BBRRRRGGGGGRR
compilation info
prog.cpp: In function ‘void processString(std::__cxx11::string&, int&, char)’:
prog.cpp:11:37: error: return-statement with a value, in function returning 'void' [-fpermissive]
  if(sameColor.length() == 0) return "";
                                     ^~
prog.cpp:13:37: error: return-statement with a value, in function returning 'void' [-fpermissive]
  if(sameColor.length() == 1) return sameColor;
                                     ^~~~~~~~~
stdout
Standard output is empty