forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
58 lines (44 loc) · 1.07 KB
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
// Implementation Part
long repeatedString(string s, long n) {
// calculate size of string s
// initialize count of a's = 0 at starting
// A will find how many times we need to repeat s
long size = s.size();
long count = 0;
long A = (n / size);
// count a's in string s
for(long i = 0; i < s.size(); i++){
if(s[i] == 'a')
count++;
}
// Case: 1
if(A * size == n)
return (A * count);
// Case: 2
else{
// get size remaining = n - A * size
// count a's in remaining size
long result = A * count;
size = n - A * size;
for(long i = 0; i < size; i++){
if(s[i] == 'a')
result++;
}
return result;
}
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string s;
getline(cin, s);
long n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
long result = repeatedString(s, n);
fout << result << "\n";
fout.close();
return 0;
}