-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_0792_numMatchingSubseq.cc
More file actions
102 lines (96 loc) · 2.76 KB
/
Copy pathProblem_0792_numMatchingSubseq.cc
File metadata and controls
102 lines (96 loc) · 2.76 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int nearLeftIndex(vector<int> &arr, int value)
{
int L = 0;
int R = arr.size() - 1;
int index = -1;
while (L <= R)
{
int M = L + (R - L) / 2;
if (arr[M] > value)
{
index = M;
R = M - 1;
}
else
{
L = M + 1;
}
}
return index;
}
int numMatchingSubseq(string s, vector<string> &words)
{
int ans = 0;
int n = words.size();
int m = s.length();
unordered_map<char, vector<int>> map;
// 记录每个字符在s出现的索引,同一个字符的vector内index一定是递增的
for (int i = 0; i < m; i++)
{
map[s[i]].push_back(i);
}
for (string &word : words)
{
bool isIncreasing = true;
int pre = -1; // 记录上个字符在s出现的位置
for (int i = 0; i < word.length() && isIncreasing; i++)
{
// 针对每一个单词的字符
char c = word[i];
vector<int> &idxs = map[c]; // 这里不用引用,LeetCode提交会超时
// 找到当前字符在s的索引,这个索引必须大于上个字符在s的索引
int cur = nearLeftIndex(idxs, pre);
if (cur < 0 || idxs[cur] <= pre)
{
// 没有这个索引
isIncreasing = false;
}
else
{
// 更新本次的索引
pre = idxs[cur];
}
}
if (isIncreasing)
{
// 单词遍历结束,索引都符合递增条件
ans++;
}
}
return ans;
}
};
void testNumMatchingSubseq()
{
Solution s;
vector<string> w1 = {"a", "bb", "acd", "ace"};
vector<string> w2 = {"ahjpjau", "ja", "ahbwzgqnuk", "tnmlanowax"};
vector<string> w3 = {"wpddkvbnn",
"lnagtva",
"kvbnnuglnagtvamxkqtwhqgwbqgfbvgkwyuqkdwhzudsxvju",
"rwpddkvbnnugln",
"gloeofnpjqlkdsqvruvabjrikfwronbrdyyj",
"vbgeinupkvgmgxeaaiuiyojmoqkahwvbpwugdainxciedbdkos",
"mspuhbykmmumtveoighlcgpcapzczomshiblnvhjzqjlfkpina",
"rgmliajkiknongrofpugfgajedxicdhxinzjakwnifvxwlokip",
"fhepktaipapyrbylskxddypwmuuxyoivcewzrdwwlrlhqwzikq",
"qatithxifaaiwyszlkgoljzkkweqkjjzvymedvclfxwcezqebx"};
EXPECT_EQ_INT(3, s.numMatchingSubseq("abcde", w1));
EXPECT_EQ_INT(2, s.numMatchingSubseq("dsahjpjauf", w2));
EXPECT_EQ_INT(5, s.numMatchingSubseq("rwpddkvbnnuglnagtvamxkqtwhqgwbqgfbvgkwyuqkdwhzudsxvjubjgloeofnpjqlkdsqvruvabjrikfwronbrdyyjnakstqjac", w3));
EXPECT_SUMMARY;
}
int main()
{
testNumMatchingSubseq();
return 0;
}