Lily has a chocolate bar that she wants to share it with Ron for his birthday. Each of the squares has an integer on it. She decides to share a contiguous segment of the bar selected such that the length of the segment matches Ron's birth month and the sum of the integers on the squares is equal to his birth day. You must determine how many ways she can divide the chocolate.
Consider the chocolate bar as an array of squares, . She wants to find segments summing to Ron's birth day, with a length equalling his birth month, . In this case, there are two segments meeting her criteria: and .
Function Description
Complete the birthday function in the editor below. It should return an integer denoting the number of ways Lily can divide the chocolate bar.
birthday has the following parameter(s):
- s: an array of integers, the numbers on each of the squares of chocolate
- d: an integer, Ron's birth day
- m: an integer, Ron's birth month
Input Format
The first line contains an integer , the number of squares in the chocolate bar.
The second line contains space-separated integers , the numbers on the chocolate squares where .
The third line contains two space-separated integers, and , Ron's birth day and his birth month.
Constraints
- , where ()
Output Format
Print an integer denoting the total number of ways that Lily can portion her chocolate bar to share with Ron.
Sample Input 0
5
1 2 1 3 2
3 2
Sample Output 0
2
Explanation 0
Lily wants to give Ron squares summing to . The following two segments meet the criteria:
Sample Input 1
6
1 1 1 1 1 1
3 2
Sample Output 1
0
Explanation 1
Lily only wants to give Ron consecutive squares of chocolate whose integers sum to . There are no possible pieces satisfying these constraints:
Thus, we print as our answer.
Sample Input 2
1
4
4 1
Sample Output 2
1
Explanation 2
Lily only wants to give Ron square of chocolate with an integer value of . Because the only square of chocolate in the bar satisfies this constraint, we print as our answer.
Solution
#includeusing namespace std; string ltrim(const string &); string rtrim(const string &); vector split(const string &); // Complete the birthday function below. int birthday(vector s, int d, int m) { int count = 0; for(int i = 0; i < s.size() - m + 1; ++i) { int startNum = i; int sum = 0; for(int j = 0; j < m; ++j) { sum += s[startNum]; ++startNum; } if(d == sum) { ++count; } } return count; } int main() { ofstream fout(getenv("OUTPUT_PATH")); string n_temp; getline(cin, n_temp); int n = stoi(ltrim(rtrim(n_temp))); string s_temp_temp; getline(cin, s_temp_temp); vector s_temp = split(rtrim(s_temp_temp)); vector s(n); for (int i = 0; i < n; i++) { int s_item = stoi(s_temp[i]); s[i] = s_item; } string dm_temp; getline(cin, dm_temp); vector dm = split(rtrim(dm_temp)); int d = stoi(dm[0]); int m = stoi(dm[1]); int result = birthday(s, d, m); 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 (isspace))) ); return s; } string rtrim(const string &str) { string s(str); s.erase( find_if(s.rbegin(), s.rend(), not1(ptr_fun (isspace))).base(), s.end() ); return s; } vector split(const string &str) { vector tokens; string::size_type start = 0; string::size_type end = 0; while ((end = str.find(" ", start)) != string::npos) { tokens.push_back(str.substr(start, end - start)); start = end + 1; } tokens.push_back(str.substr(start)); return tokens; }
출저 : https://www.hackerrank.com/challenges/the-birthday-bar/problem
'취미 > Programming Problems' 카테고리의 다른 글
[Codility] OddOccurrencesInArray (0) | 2023.05.02 |
---|---|
[백준] 2677번 - 단지 번호 붙이기 (0) | 2020.07.20 |
[ Hacker Rank ] 기록 깨기(Breaking the Records) (0) | 2018.08.22 |
[ 백준 ] 1065번 - 한수 (0) | 2018.07.27 |
[ Hacker Rank ] 캥거루(Kangaroo) (0) | 2018.07.25 |