I coding interview for c++, Topic name’s Sliding window
#include
#include
#include
#include<bits/stdc++.h>
using namespace std;int main() { unordered_map<string , unsigned int> map; string s = "barfoothefoobarman"; vector<string> words = {"bar","foo","mannn"} ; vector<int> result; unsigned int length = words[0].size(); map.clear(); for(const string & word : words) { map[word]++; } //iterate for(unsigned int offset = 0 ; offset < length; offset++) { //silding window size unsigned size = 0; unordered_map<string,unsigned int> seen; for(unsigned int i = offset; i + length <= s.size(); i += length) { cout<<endl<<i<<" |"<<endl<<offset<<endl; string sub = s.substr(i, length); //if the word absent int the reference map // clear silding window and move on auto itr = map.find(sub); if(itr == map.end()) { cout<<"Not f: "<<sub<<endl; size = 0; continue; } else { cout<<"found : "<<sub<<"t"<<map[sub]<<endl; // map[sub] is counter occurrences } //Increase seen[sub]++; cout<<"Seen :"<<seen[sub]<<endl; size++; // To make sure the silding window is valid // We need to check only recent occurence // againts the reference // because previous occurence // were checked on previous iterations cout<<seen[sub]<<"t seen and itr t"<<itr->second<<endl; while(seen[sub] > itr -> second) { // If the occurrences amount exceeds // the reference amount, // we shrink the window from the left until // the window becomes valid again. // A word at the beginning of the current sliding window. string first = s.substr(i - (size - 1) * length, length); cout<<"Size-1 * length :"<<(size-1)*length<<endl<<"First :"<<first<<endl; // remove the occurrence from the window // skrinking it from the left --seen[first]; --size; // need to calculate the begining // index of the current sliding window if(size == words.size()) { cout<<"oke"; result.push_back(i-(size-1)*length); } } } } for(int i : result) { cout<<"result : "<<i<<endl; } }
I encounter problem in the result, it indicates a variable’s result.
I try run code, which successfully complier
But not result my result hope doing true
Don’t run a success