Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of S.

Example :
Input:
S = "abcde"
words = ["a", "bb", "acd", "ace"]
Output: 3
Explanation: There are three words in words that are a subsequence of S: "a", "acd", "ace".
Note: All words in words and S will only consists of lowercase letters.
The length of S will be in the range of [1, 50000].
The length of words will be in the range of [1, 5000].
The length of words[i] will be in the range of [1, 50].

思路:先用vector记录每个字母出现的位置,然后遍历每个单词,如果每个字母的位置是递增的那么这个单词就符合要求。如何判断位置见的递增关系呢?可以用二分实现。具体看代码

class Solution {
public:
int numMatchingSubseq(string S, vector<string>& words) {
vector<int> v[26];
for (int i = 0; i < S.size(); ++i) {
v[S[i]-'a'].emplace_back(i);
}
int ans = 0;
for (auto i : words) {
int mark = 0;
int y = -1;
for(auto c : i) {
int x = c - 'a';
if (v[x].empty()) {
mark = 1; break;
}
int pos = upper_bound(v[x].begin(), v[x].end(), y) - v[x].begin();
if (pos < v[x].size()) {
y = v[x][pos];
} else {
mark = 1; break;
}
}
if (!mark) ans++;
}
return ans;
}
};

最新文章

  1. DevExpress学习系列(控件篇):GridControl的基本应用
  2. 初学python第二天
  3. C#调用c++Dll 结构体数组指针的问题
  4. js 格式化数字保留2位小数
  5. Android广播机制:Broadcast
  6. nginx笔记----安装
  7. 为 vsftpd 启动 vsftpd:500 OOPS: bad bool value in config file for: pasv_enable
  8. Deep Learning深入研究整理学习笔记五
  9. React Native与原生项目连接与发布
  10. Spring事务隔离级别
  11. &lt;script&gt;的用法
  12. LxmlLinkExtractor类参数解析
  13. C# BackJson Beautiful format
  14. 动态规划dp
  15. Mtlab:抛物型方程的交替方向隐格式(ADI)
  16. 剑指offer——从上往下打印二叉树
  17. Codeforces Round #449 (Div. 2)
  18. Java坦克大战(一)
  19. 【贪心】PAT 1033. To Fill or Not to Fill (25)
  20. MapReduce的计数器

热门文章

  1. 安卓WebView在项目中总结
  2. html-禁用右键、键盘F12、网页上选取内容、复制、粘贴
  3. PostgreSQL 二进制安装
  4. T2821 天使之城 codevs
  5. git常用语句
  6. 板子-GOD BLESS ALL OF YOU
  7. springboot idea激活指定profile
  8. iOS Framework: Introducing MKNetworkKit (MKNetworkKit介绍,入门,翻译)
  9. Concurrency and Application Design (一)
  10. uitableview使用reloaddata不管用