这是亚麻2018 年新题的第一题:
// find all the N substring with only one duplicate character. #include <iostream> // std::cout #include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap #include <vector> // std::vector #include <unordered_map> #include <unordered_set> #include <numeric> #include <sys/time.h> //use a slide window to mared each matched range and record the optimal result. //use the hash map to index the each substring in target. //iterate all substring in available container and find out each slide window that is marked with the updated indexs stored in a vector container. using namespace std; std::vector<string> subStringKDistance(string& str, int K){ int s =0; int e = K-1; unordered_map<char, int> hashmap; vector<string> res; unordered_set<string> check; while (e < str.size()){ hashmap.clear(); //check if there is duplicated char in the slided window by hahtable. for ( int p = s; p <= e; p++){ hashmap[str[p]] += 1; } for (auto& e: hashmap){ if (e.second >= 2){ // have duplicate char. goto CONTINUE; } } //no duplicate, so check is already existed in the vector using hashtable. if not , insert into the vector. // if ( check.find(str.substr(s,K)) == check.end()){ // check.insert(str.substr(s,K)); // res.push_back(str.substr(s, K)); // } // //check if duplicate using == of two substring. comparing with the hashtable, the effeciency is almost same. for (auto& e: res){ if (e == str.substr(s,K)) goto CONTINUE; } res.push_back(str.substr(s, K)); CONTINUE: //move the slide window. s++; e++; } //this method change the index of each substring, so how to delete the duplicated substring effeciently? // sort(res.begin(),res.end()); // unique(res.begin(),res.end()); // how to delete the duplicate substring effeciently? // solution : while insert a substring into vector, check a set to see if it is already existed in the set? // if yes, that means that susbstring is already in vector, otherwise,it is not. return res; } int main () { struct timeval tv; gettimeofday(&tv,NULL); long ts = tv.tv_sec * 1000 + tv.tv_usec / 1000; string s ={"awaglknagawunagwkwagl"}; cout<<"length of the string: " << s.size() << endl; vector<string>&& out = subStringKDistance(s, 4); for (auto& e : out){ cout<< e << endl; } gettimeofday(&tv,NULL); long te = tv.tv_sec * 1000 + tv.tv_usec / 1000; cout<< "running tmie is : " << te - ts << endl; return 0; }
原文地址:https://www.cnblogs.com/HisonSanDiego/p/8283285.html
时间: 2024-11-25 23:16:28