**LeetCode 30. Substring with Concatenation of All Words
来源:互联网 发布:淘宝网站结构图 编辑:程序博客网 时间:2024/06/06 05:04
https://leetcode.com/problems/substring-with-concatenation-of-all-words/
滑动窗口法。解析看这里:
http://www.2cto.com/kf/201406/311648.html
算法时间复杂度为O(n*k))n是字符串的长度,k是单词的长度
#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <map>using namespace std;class Solution {public: vector<int> findSubstring(string s, vector<string>& words) { map<string,int> wd; for(int i=0;i<words.size();i++) wd[ words[i] ] ++; vector <int> ret; if( words.size() == 0 || words[0].size() > s.size() )return ret; int sz = words[0].size(); for(int i=0;i<sz;i++) { int winSt = i, cnt=0; map <string, int> found; for(int winEnd = i; winEnd <= s.size()-sz; winEnd += sz ) { //s.size()-sz because word = s.substr(winEnd, sz); //也就是最后一个位置winEnd开始放words[0] string word = s.substr(winEnd, sz); if(wd.find(word) != wd.end()) { if(wd[word] > found[word]) { found[word] ++; cnt ++; } else { string tmp; while( (tmp=s.substr(winSt, sz)) != word ) { winSt += sz; found[tmp] --; cnt --; } winSt += sz; //here can't because -- and insert word , so you needn't do anything except winSt+=sz //found[tmp] --; //cnt --; } if(cnt == words.size()) { //从下一个位置开始 ret.push_back(winSt); found[ s.substr(winSt, sz) ] --; winSt += sz; cnt --; } } else { found.clear(); winSt = winEnd + sz; cnt=0; } } } return ret; }};int main() { freopen("l30.txt", "r", stdin); int n; string s,in; while(cin >> n) { vector<string>svec; for(int i=0;i<n;i++) { cin >> in; svec.push_back(in); } cin >> s; Solution so; vector<int>ans = so.findSubstring(s, svec); for(int i=0;i<ans.size();i++) { cout << ans[i] << ", "; } cout << "ans end" << endl; cout << endl; } return 0;}
1 0
- LeetCode: Substring with Concatenation of All Words
- LeetCode : Substring with Concatenation of All Words
- [Leetcode] Substring with Concatenation of All Words
- [LeetCode]Substring with Concatenation of All Words
- LeetCode-Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- LeetCode:Substring with Concatenation of All Words
- Leetcode: Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- LeetCode Substring with Concatenation of All Words
- LeetCode | Substring with Concatenation of All Words
- LeetCode - Substring with Concatenation of All Words
- Substring with Concatenation of All Words -- LeetCode
- Leetcode: Substring with Concatenation of All Words
- leetcode:Substring with Concatenation of All Words
- LeetCode|Substring with Concatenation of All Words
- Docker问题
- socket编程 -- 基于TCP协议的C/S通信模型及实现
- leetcode:Longest Valid Parentheses
- 【高斯消元】[JSOI2008][HYSBZ/BZOJ1013]球形空间产生器sphere
- Eclipse快捷键
- **LeetCode 30. Substring with Concatenation of All Words
- Maven: 每次更新Maven Project ,JAVA 版本都变为1.5
- 在腾讯的第三堂产品课——爆品的秘密 梁宁-闲花照水录
- BNUOJ 51275 道路修建(并查集)
- Maven: 互联网开发常用的jar以及版本pom.xl文件
- 使用python开源组件yarn-api-client获取yarn信息
- 前端实战——完美解决文本框超出内容用“……”表示,鼠标滑过时显示隐藏内容
- 华为机试题:输入整型数组和排序标识,对其元素按照升序或降序进行排序
- 腾讯的职级系统 梁宁-闲花照水录