Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 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, cc.begin()+pos +len+1);
  27.            
  28.         }
  29.         //cout << "pos = " << pos << " , len = " << len << endl;
  30.        
  31.         len = 0;
  32.         pos++;
  33.     }
  34.     string output(cc.begin(),cc.end());
  35.     return output;
  36. }
  37.  
  38. void CandyCrush(string &input) {
  39.     vector<char> cc(input.begin(), input.end());
  40.     //while (input != RemoveRow(cc)) {
  41.     //  input = RemoveRow(cc);
  42.     //  cout << "N-te czyszczenie: " <<endl<< input << endl;
  43.     //}
  44.     for (int i = 0; i < 10; i++) {
  45.        
  46.         cout << "N-te czyszczenie: " << endl << RemoveRow(cc) << endl;
  47.     }
  48.  
  49. }
  50.  
  51.  
  52.  
  53. int main()
  54. {
  55.     string input = "BABBBBBAACAAACC";
  56.    
  57.     cout<<"Input string: "<<endl<<input<<endl;
  58.     CandyCrush(input);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement