Repeated String
#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); /* * Complete the 'repeatedString' function below. * * The function is expected to return a LONG_INTEGER. * The function accepts following parameters: * 1. STRING s * 2. LONG_INTEGER n */ long repeatedString(string s, long n) { /* * create the long string * for(n amount) for(i < s.size()) append s to s * for(n amount) count the a's */ //string newStr; long count = 0; long j = 0; // account for all options being "a" bool allA = true; int countA = 0; // gets number of 'a' in string // Print commands ######################## cout << "Stage: Starting\n"; cout << "Count: " << count << "\n"; cout << "CountA: " << countA << "\n\n"; // ####################################### for(long i = 0; i < s.size(); i++){ if(s.at(i) != 'a'){ allA = false; } else if(s.at(i) == 'a'){ countA++; } } if(allA){ return n; } // Print commands ######################## cout << "Stage: Calculating number of A's\n"; cout << "Count: " << count << "\n"; cout << "CountA: " << countA << "\n\n"; // ####################################### // divide str size by n and check remainder // check the remainder for instances of 'a' // checks if n is greater than size before dividing if(n > s.size()) { count += n/s.size()*countA; } // Print commands ######################## cout << "Stage: Checking size of n and adding divided count\n"; cout << "Count: " << count << "\n"; cout << "CountA: " << countA << "\n\n"; // ####################################### int remainder = n % s.size(); for(int i = 0; i < remainder; i++){ if(s.at(i) == 'a'){ count++; } } // Print commands ######################## cout << "Stage: After adding remainder\n"; cout << "Remainder: " << remainder << "\n"; cout << "Count: " << count << "\n"; cout << "CountA: " << countA << "\n\n"; // ####################################### return count; } int main() { ofstream fout(getenv("OUTPUT_PATH")); string s; getline(cin, s); string n_temp; getline(cin, n_temp); long n = stol(ltrim(rtrim(n_temp))); long result = repeatedString(s, n); fout << result << "\n"; fout.close(); return 0; } string ltrim(const string &str) { string s(str); s.erase( s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))) ); return s; } string rtrim(const string &str) { string s(str); s.erase( find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), s.end() ); return s; }
Leave a Comment