interviews/training->training
This commit is contained in:
107
puzzles/training/unique_prefix.cpp
Normal file
107
puzzles/training/unique_prefix.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I."
|
||||
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
|
||||
VIM-: let b:cppflags=g:Iboost.g:Itbb
|
||||
VIM-: let b:ldflags=g:Lboost.g:Ltbb
|
||||
VIM-: let b:ldlibpath=g:Bboost.g:Btbb
|
||||
VIM-: let b:argv=""
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
/*
|
||||
PREFIX
|
||||
|
||||
Find shortest unique prefix to represent each word in the list.
|
||||
|
||||
Example:
|
||||
|
||||
Input: [zebra, dog, duck, dove]
|
||||
Output: {z, dog, du, dov}
|
||||
where we can see that
|
||||
zebra = z
|
||||
dog = dog
|
||||
duck = du
|
||||
dove = dov
|
||||
|
||||
NOTE : Assume that no word is prefix of another. In other words, the
|
||||
representation is always possible.
|
||||
|
||||
*/
|
||||
|
||||
std::string get_prefix(const std::string& p1, const std::string& p2){
|
||||
for ( int i = 0; i < p1.size() && i < p2.size(); ++i){
|
||||
if ( p1[i] != p2[i] ){
|
||||
return p1.substr(0,i+1);
|
||||
}
|
||||
}
|
||||
return p1;
|
||||
}
|
||||
|
||||
std::vector<std::string> prefix(const std::vector<std::string> &A) {
|
||||
// Do not write main() function.
|
||||
// Do not read input, instead use the arguments to the function.
|
||||
// Do not print the output, instead return values as specified
|
||||
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
|
||||
|
||||
std::vector<std::string> ret(A.size());
|
||||
|
||||
std::vector<int> B(A.size());
|
||||
for ( size_t i = 0; i < A.size(); ++i){
|
||||
B[i] = i;
|
||||
}
|
||||
std::sort(B.begin(), B.end(), [&A](int op1, int op2){
|
||||
return A[op1] < A[op2];
|
||||
});
|
||||
|
||||
for (int i = 0; i < B.size(); ++i){
|
||||
std::string p;
|
||||
if ( i-1 >= 0 ) {
|
||||
p = get_prefix(A[B[i]],A[B[i-1]]);
|
||||
}
|
||||
if ( i+1 < A.size() ){
|
||||
auto _p = get_prefix(A[B[i]],A[B[i+1]]);
|
||||
if ( _p.size() > p.size() ) {
|
||||
p = _p;
|
||||
}
|
||||
}
|
||||
if ( p.empty() ) {
|
||||
p = A[B[i]].substr(0,1);
|
||||
}
|
||||
ret[B[i]] = p;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int main ( void )
|
||||
{try{
|
||||
auto begin = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto p = prefix({"zebra", "dog", "duck", "dove"});
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> seconds = end - begin;
|
||||
std::cout << "Time: " << seconds.count() << std::endl;
|
||||
for ( const auto& _p : p) {
|
||||
std::cout << _p << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
catch ( const std::exception& e )
|
||||
{
|
||||
std::cerr << std::endl
|
||||
<< "std::exception(\"" << e.what() << "\")." << std::endl;
|
||||
return 2;
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
std::cerr << std::endl
|
||||
<< "unknown exception." << std::endl;
|
||||
return 1;
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user