class Solution {
public:
int bsearch(int L,int R,vector<int>& V,int key){
//printf("<%d %d %d>\n",L,R,key);
if(L+1>=R) return R;
int mid = (L+R)/2;
if(V[mid] <= key) return bsearch(mid,R,V,key);
return bsearch(L,mid,V,key);
}
int numMatchingSubseq(string S, vector<string>& words) {
vector<vector<int>> V(26,vector<int>(0));
for(int i=0;i<26;i++) V[i].push_back(-1);
for(int i=0;i<S.length();i++){
V[S[i]-'a'].push_back(i);
}
int ans = 0;
for(int i=0;i<words.size();i++){
int lastloc = -1;
bool flag = true;
//cout<<words[i]<<endl;
for(int j=0;j<words[i].length();j++){
int index = words[i][j]-'a';
int nowloc = bsearch(0,V[index].size(),V[index],lastloc);
if(nowloc == V[index].size()){
flag = false;
break;
}
lastloc = V[index][nowloc];
}
if(flag) ans++;
}
return ans;
}
};
No.162 - LeetCode792 - 字符串子序列匹配(下标挂链二分)
最新推荐文章于 2022-11-17 21:58:33 发布