Skip to content

Commit cc96ec7

Browse files
committed
leetcode 2/6/2014
1 parent a44cf5a commit cc96ec7

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

‎LeetCode/findSubstring.cpp‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <map>
4+
#include <set>
5+
using namespace std;
6+
7+
#define REP(i,n) for(int i=0;i<(n);++i)
8+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
9+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
10+
11+
class Solution {
12+
public:
13+
vector<int> findSubstring(string S, vector<string> &L) {
14+
int n = L.size(), len = L[0].length();
15+
map<string, int> mm;
16+
map<string, int> t;
17+
REP(i,n) {
18+
if (mm.find(L[i]) == mm.end()) mm[L[i]] = 1;
19+
else ++mm[L[i]];
20+
}
21+
22+
vector<int> ret;
23+
RFOR(pos,S.length() - n * len, 0) {
24+
t.clear();
25+
bool isok = true;
26+
for (int i = (n - 1) * len; i >= 0; i -= len) {
27+
string str = S.substr(i + pos, len);
28+
if (mm.find(str) == mm.end()) {
29+
isok = false;
30+
break;
31+
}
32+
++t[str];
33+
if (t[str] > mm[str]) {
34+
isok = false;
35+
break;
36+
}
37+
}
38+
if (isok) ret.push_back(pos);
39+
}
40+
41+
return ret;
42+
}
43+
};
44+
45+
int main() {
46+
Solution s = Solution();
47+
vector<string> L;
48+
L.push_back("a");
49+
L.push_back("b");
50+
L.push_back("a");
51+
vector<int> ret = s.findSubstring("abababab", L);
52+
REP(i,ret.size()) cout << ret[i] << endl;
53+
return 0;
54+
}

0 commit comments

Comments
 (0)