This commit is contained in:
Vahagn Khachatryan
2019-03-03 23:19:15 +00:00
5 changed files with 416 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
/*
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 <stdexcept>
void print_exception()
{
try {
std::rethrow_exception(std::current_exception());
} catch ( const std::exception& e ) {
std::cout << e.what() << std::endl;
}
}
int main ( void )
{try{
auto begin = std::chrono::high_resolution_clock::now();
try {
throw std::runtime_error("first exception");
} catch(...) {
try {
throw std::runtime_error("second exception");
} catch (...) {
print_exception();
}
print_exception();
}
//......
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> seconds = end - begin;
std::cout << "Time: " << seconds.count() << 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;
}}

View File

@@ -0,0 +1,54 @@
/*
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 <future>
int main ( void )
{try{
auto begin = std::chrono::high_resolution_clock::now();
std::promise<int> p;
auto f = p.get_future().share();
std::thread t1([f](){
std::cout << ' ' << f.get() << std::endl;
});
std::thread t2([f](){
std::cout << ' ' << f.get() << std::endl;
});
p.set_value(55);
t1.join();
t2.join();
//......
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> seconds = end - begin;
std::cout << "Time: " << seconds.count() << 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;
}}

View File

@@ -0,0 +1,121 @@
/*
VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I."
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM-: let b:argv=""
*/
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
/*
* Complete the function below.
*/
bool match(const string& pattern, const string& string) {
auto s = string.begin();
auto p = pattern.begin();
const std::string::const_iterator empty;
auto prev_p = empty;
auto prev_s = s;
while (s != string.end()) {
while ( p != pattern.end() && *p == '*' ) {
p++;
prev_p = p;
prev_s = s;
}
if ( p == pattern.end() ) {
if ( prev_p == p ){
return true;
} else if ( prev_p != empty ) {
p = prev_p;
s = ++prev_s;
} else {
return false;
}
}
else if ( *s == *p ){
s++;
p++;
}
else if ( *p == '?' ){
s++;
p++;
}
else if ( prev_p != empty ) {
p = prev_p;
s = ++prev_s;
}
else {
return false;
}
}
while ( *p == '*' ) {
p++;
}
return p == pattern.end();
}
void test( const std::string& str, const std::string& pattern ){
bool res = match(pattern, str);
std::cout << str << " - " << pattern << " -> " << res <<std::endl;
}
int main() {
#if 1
test( "", "" );
test( "", "*" );
test( "", "?" );
test( "a", "?" );
test( "abc", "abc" );
test( "aaaaabc", "*abc" );
test( "aaaaabc", "a*bc" );
test( "aaaaabc", "*abc*" );
test( "abc", "*abc*" );
test( "abc", "****abc*" );
test( "abcabaabcabacabc", "a*ba*c" );
test( "abcabcabcabccabc", "a*b*cc" );
test( "abcabcabcabccabc", "a*b*c?" );
test( "abcabcabcabccabc", "a*b*b?*" );
test( "abcabcabcabccabc", "*a*?*c*" );
test( "abcabcabcabccabc", "****a****??????*c******" );
#else
ofstream fout(getenv("OUTPUT_PATH"));
bool res;
string _pattern;
getline(cin, _pattern);
string _string;
getline(cin, _string);
res = match(_pattern, _string);
fout << res << endl;
fout.close();
#endif
return 0;
}

View File

@@ -0,0 +1,89 @@
/*
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>
template <int N>
struct power_of
{
enum {
value = power_of<(N+1)/2>::value * 2
};
};
template <>
struct power_of<2>
{
enum {
value = 2
};
};
template <>
struct power_of<1>
{
enum {
value = 1
};
};
template <>
struct power_of<0>
{
enum {
value = 0
};
};
template <int N>
void test()
{
std::cout << N << "=> " << power_of<N>::value <<std::endl;
}
int main ( void )
{try{
auto begin = std::chrono::high_resolution_clock::now();
test<0>();
test<1>();
test<2>();
test<3>();
test<4>();
test<5>();
test<6>();
test<7>();
test<8>();
test<9>();
test<127>();
test<128>();
test<129>();
//......
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> seconds = end - begin;
std::cout << "Time: " << seconds.count() << 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;
}}

View File

@@ -0,0 +1,95 @@
/*
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 <string>
/*
Simple wildcard match.
*/
bool wildcard_match(const std::string& input, const std::string& pattern)
{
int j = 0;
int i = 0;
int restart_idx = -1;
while ( i < input.size() )
{
while ( j < pattern.size() && pattern[j] == '*' ){
restart_idx = ++j;
}
if ( j<pattern.size() && pattern[j] == input[i] ) {
++i;
++j;
}
else if ( j>= 0 && pattern[j-1] == '*' ) {
++i;
}
else if ( restart_idx > 0 ) {
i -= j - restart_idx -1;
j = restart_idx;
}
else {
return false;
}
}
while ( j < pattern.size() && pattern[j] == '*' ){
++j;
}
return j == pattern.size();
}
void test(const std::string& input, const std::string& pattern)
{
std::cout << input << " x " << pattern
<< " == " << wildcard_match(input,pattern)
<< std::endl;
}
int main ( void )
{try{
auto begin = std::chrono::high_resolution_clock::now();
test("zebra", "dog");
test("zebra", "zebra");
test("zebra", "*br*");
test("zebra", "*bi*");
test("zebra", "*br*a");
test("zebra", "*br*b");
test("zebra", "z**a");
test("zebra", "z*a");
test("zebra", "x**a");
test("zebrabrb", "*brb*");
test("zebra", "*bra*");
test("zebrazebrazebra", "*bra*zeb*zeb*");
test("zebrbzebrazebra", "*bra*zeb*zeb*");
test("zebrbzebrazebra", "*bra*zeb*");
test("zebrbzebrazebra", "*****bra*zeb*****");
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> seconds = end - begin;
std::cout << "Time: " << seconds.count() << 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;
}}