-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkRepeatedLongestSubstring.h
74 lines (69 loc) · 1.94 KB
/
kRepeatedLongestSubstring.h
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
//
// Created by so_go on 2019/6/14.
//
#ifndef SRC_KREPEATEDLONGESTSUBSTRING_H
#define SRC_KREPEATEDLONGESTSUBSTRING_H
#include"printVector.h"
#include<string>
#include<vector>
#include<array>
using namespace std;
class KRepeatedLongestSubstring {
public:
int longestSubstring(string s, int k) {
return partition(s, 0, s.size(), k);
}
int partition(string &s, int i, int j, int k){
/* 遍历计数,对于重复次数小于k的字符,在对应位置分割,
* 对分割得到的所有子串,重复以上操作,取所有分割子串返回值的最大值,
* 若重复次数均大于k,无需再次分割,返回子串长度
*
* 在s[i, j)中搜索
*
* 若 j - i < k 则返回0
*/
//截止条件
cout << i << ' ' << j << endl;
if( j - i < k ){
return 0;
}
vector<int> count(26, 0);
for(int it = i; it < j; it++){
count[s[it] - 'a']++;
}
printVector(count);
// 截止2
bool flag = true;
for(int c: count){
if(c < k and c > 0){
flag = false;
break;
}
}
if(flag){
return j - i;
}
else{
//遍历字符串,寻找下一分割点
int last_cut = i;
int max = 0, tmp;
for(int it = i; it < j; it++){
if(count[s[it] - 'a'] < k){
tmp = partition(s, last_cut, it, k);
if(tmp > max){
max = tmp;
}
last_cut = it + 1;
}
}
if(last_cut != j){
tmp = partition(s, last_cut, j, k);
if(tmp > max){
max = tmp;
}
}
return max;
}
}
};
#endif //SRC_KREPEATEDLONGESTSUBSTRING_H