Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- //
- //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 -
- //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
- //groups of letters have been removed.
- //
- //Example :
- // Input string : AABBCCCCBDDDADB
- // After first round : AABBBADB
- // After second round : AAADB
- // After third round : DB
- // Output : DB
- string RemoveRow(vector<char> &cc) {
- int pos=0, len=0;
- while (pos<cc.size()) {
- while (pos+len+1<cc.size() && (cc[pos] == cc[pos + len + 1])) {
- len++;
- }
- if (len > 1) {
- cc.erase(cc.begin()+pos, cc.begin()+pos +len+1);
- }
- //cout << "pos = " << pos << " , len = " << len << endl;
- len = 0;
- pos++;
- }
- string output(cc.begin(),cc.end());
- return output;
- }
- void CandyCrush(string &input) {
- vector<char> cc(input.begin(), input.end());
- //while (input != RemoveRow(cc)) {
- // input = RemoveRow(cc);
- // cout << "N-te czyszczenie: " <<endl<< input << endl;
- //}
- for (int i = 0; i < 10; i++) {
- cout << "N-te czyszczenie: " << endl << RemoveRow(cc) << endl;
- }
- }
- int main()
- {
- string input = "BABBBBBAACAAACC";
- cout<<"Input string: "<<endl<<input<<endl;
- CandyCrush(input);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement