forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindSubstring.cpp
More file actions
54 lines (49 loc) · 1.35 KB
/
findSubstring.cpp
File metadata and controls
54 lines (49 loc) · 1.35 KB
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
#include <iostream>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
class Solution {
public:
vector<int> findSubstring(string S, vector<string> &L) {
int n = L.size(), len = L[0].length();
map<string, int> mm;
map<string, int> t;
REP(i,n) {
if (mm.find(L[i]) == mm.end()) mm[L[i]] = 1;
else ++mm[L[i]];
}
vector<int> ret;
RFOR(pos,S.length() - n * len, 0) {
t.clear();
bool isok = true;
for (int i = (n - 1) * len; i >= 0; i -= len) {
string str = S.substr(i + pos, len);
if (mm.find(str) == mm.end()) {
isok = false;
break;
}
++t[str];
if (t[str] > mm[str]) {
isok = false;
break;
}
}
if (isok) ret.push_back(pos);
}
return ret;
}
};
int main() {
Solution s = Solution();
vector<string> L;
L.push_back("a");
L.push_back("b");
L.push_back("a");
vector<int> ret = s.findSubstring("abababab", L);
REP(i,ret.size()) cout << ret[i] << endl;
return 0;
}