-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepeated String.cpp
86 lines (65 loc) · 1.46 KB
/
Repeated String.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
// Complete the repeatedString function below.
long repeatedString(string s, long long n) {
long size= s.length();
int flag=0;
long answer=0;
for(int i=0;i<size;i++)
{
if(s[i]=='a')
{
flag=1; //flag =0 means no 'a' flag=1 means 'a' present.
break;
}
}
//when there is a
if( flag==1)
{
if(size==1)
{
cout<<"bah";
answer = n ;
}
else
{
cout<<"cah";
int c=0;
//finding a in the og string.
for(int i=0;i<size;i++)
{
if(s[i]=='a')
{
c++;
}
}
long long m=n/size;
long long k= n%size;
long long t=0;
long long y= (c*m);
for(int i=0;i<k;i++)
{
if(s[i]=='a')
{
t++;
}
}
y=y+t;
answer=y;
}
}
return answer;
}
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;
}