Created
June 18, 2019 04:27
-
-
Save kei9327/ec8a36e99a9fe3081dea67f88cacb2f3 to your computer and use it in GitHub Desktop.
HackerRank>Algorithm>implementation>birthday
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
string ltrim(const string &); | |
string rtrim(const string &); | |
vector<string> split(const string &); | |
// Complete the birthday function below. | |
int birthday(vector<int> s, int d, int m) { | |
int count = 0; | |
for (int i=0; i < s.size(); i++) { | |
int sum = 0; | |
if (i + m - 1 >= s.size()) break; | |
for (int j = i; j < i + m; j++) sum += s[j]; | |
if (sum == d) 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<string> s_temp = split(rtrim(s_temp_temp)); | |
vector<int> 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<string> 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<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; | |
} | |
vector<string> split(const string &str) { | |
vector<string> 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment