-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathCount The Repetitions.cpp
More file actions
29 lines (29 loc) · 939 Bytes
/
Count The Repetitions.cpp
File metadata and controls
29 lines (29 loc) · 939 Bytes
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
class Solution {
public:
int getMaxRepetitions(string s1, int n1, string s2, int n2) {
vector<int> rapport(102,-1);
vector<int> rest(102,-1);
int b=-1;int posRest=0;int rap=0;
int last=-1;
rapport[0]=rest[0]=0;
for(int i=1;i<=s2.size()+1;i++){
int j;
for(j=0;j<s1.size();j++){
if(s2[posRest]==s1[j]){
posRest++;
if(posRest==s2.size()){
rap++;
posRest=0;
}
}
}
for(int k=0;k<i;k++){
if(posRest==rest[k]){b=k;last=i;break;}
}
rapport[i]=rap;rest[i]=posRest;
if(b>=0)break;
}
int interval=last-b;
if(b>=n1)return rapport[n1]/n2;
return ((n1-b)/interval*(rapport[last]-rapport[b])+rapport[(n1-b)%interval+b])/n2; }
};