Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6. //
  7. //Do you know how the candy crush type games work ? Write a function that receives a string and plays a candy crush game with it -
  8. //removes letters when there are 3 or more the same letters in a row.We want this function to return what is left after all of the
  9. //groups of letters have been removed.
  10. //
  11. //Example :
  12. //  Input string : AABBCCCCBDDDADB
  13. //  After first round : AABBBADB
  14. //  After second round : AAADB
  15. //  After third round : DB
  16. //  Output : DB
  17.  
  18. string RemoveRow(vector<char> &cc) {
  19.     int pos=0, len=0;
  20.     while (pos<cc.size()) {
  21.         while (pos+len+1<cc.size() && (cc[pos] == cc[pos + len + 1])) {
  22.             len++;
  23.         }
  24.        
  25.         if (len > 1) {
  26.             cc.erase(cc.begin()+pos-1, cc.begin()+pos +len);
  27.         }
  28.         len = 0;
  29.         pos++;
  30.     }
  31.     string output(cc.begin(),cc.end());
  32.     return output;
  33. }
  34.  
  35. void CandyCrush(string &input) {
  36.     vector<char> cc(input.begin(), input.end());
  37.     while (input != RemoveRow(cc)) {
  38.         input = RemoveRow(cc);
  39.         cout << "N-te czyszczenie: " <<endl<< input << endl;
  40.     }
  41.     //for (int i = 0; i < 100; i++) {
  42.     // 
  43.     //  cout << "N-te czyszczenie: " << endl << RemoveRow(cc) << endl;
  44.     //}
  45.  
  46. }
  47.  
  48.  
  49.  
  50. int main()
  51. {
  52.     string input = "AABBCCCCBDDDADB";
  53.    
  54.     cout<<"Input string: "<<endl<<input<<endl;
  55.     CandyCrush(input);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement