drolmal
BAN USER#include <random>
#include <vector>
#include <iostream>
const int _RC_EMPTY=0;
const int _RC_EXPOSED=2;
const int _RC_MINE=1;
class Board
{
public:
Board ( size_t r, size_t c, size_t m)
{
std::srand(time(NULL));
_board = std::vector<std::vector<int>>(c,std::vector<int>(r,_RC_EMPTY));
for ( int i= 0; i< m; i++ )
{
_board[rand() %c][rand() % r] = _RC_MINE;
}
}
void PrintBoard( bool final = false )
{
for ( std::vector<int> & v : _board )
{
std::cout << "|";
for ( int i : v )
{
if ( final )
std::cout << (( i == _RC_MINE) ? "*" : " ");
else
std::cout << (( i == _RC_EXPOSED) ? "X" : " ");
std::cout << "|";
}
std::cout << std::endl;
}
}
int get( int r, int c)
{
return _board[r][c];
}
void set( int r, int c, int v)
{
_board[r][c] = v;
}
std::vector<std::vector<int>> _board;
};
class MineSweeper
{
public:
MineSweeper(size_t r, size_t c, size_t m):_board(r,c,m),_sizer(r),_sizec(c)
{
_board.PrintBoard( false );
}
int Play()
{
int r = rand() %_sizer;
int c = rand() %_sizec;
int finished = false;
if ( _board.get(c,r) == _RC_MINE ){
finished = true;
}
else{
finished = false;
_board.set(c,r, _RC_EXPOSED);
}
_board.PrintBoard( finished );
return finished;
}
Board _board;
int _sizer;
int _sizec;
};
int main(int argc, const char * argv[]) {
MineSweeper m(10,6,12);
while ( m.Play() == false );
return 0;
}
#include <fstream>
#include <sstream>
#include <string>
#include <unordered_set>
#include <vector>
unordered_set<string> GetUniqueLines(vector<string>& str )
{
unordered_set<string> res;
for ( string filename: str ){
std::string line;
std::ifstream infile(filename);
if ( !infile ) {
continue;
}
while (std::getline(infile, line)) {
std::istringstream iss(line);
res.insert(line);
}
}
return res;
}
std::vector<std::pair<string, int>>
CountWords( string str, int rank )
{
unordered_map<string,int> WordsWithRank;
size_t startpos = 0;
while ( startpos < str.size() )
{
int pos=str.find_first_of(". ;:?!", startpos);
if ( pos == string::npos )
break;
string word = str.substr(startpos,pos-startpos);
auto pp = WordsWithRank.find(word);
if ( pp != WordsWithRank.end() )
{
(*pp).second++;
}
else
WordsWithRank.insert(make_pair(word,1));
while( pos < str.size() &&
(str.substr(pos,1).find_first_of(". ;:?!")!=string::npos ) )
{
pos++;
}
startpos=pos;
}
std::vector<std::pair<string, int>> pairs;
pairs.reserve(WordsWithRank.size());
std::copy(
WordsWithRank.begin(),
WordsWithRank.end(),
std::back_inserter(pairs));
sort( pairs.begin(),
pairs.end(),
[](std::pair<string, int>& a, std::pair<string, int>& b)
{
return a.second > b.second;
}
);
int endpos = 0;
while ( ( endpos < pairs.size() ) && ( pairs[endpos].second >= rank ) )
{
endpos++;
}
pairs.resize(endpos);
return pairs;
}
class Timer{
public:
Timer():_start(0),_stop(0)
{
}
void start()
{
std::lock_guard<std::mutex> g(_mutex);
time(&_start);
}
double stop()
{
std::lock_guard<std::mutex> g(_mutex);
time(&_stop);
double res = difftime(_stop,_start);
std::cout << "Elapsed: " << res << std::endl;
return res;
}
void reset()
{
std::lock_guard<std::mutex> g(_mutex);
_start=0;
_stop=0;
}
private:
std::mutex _mutex;
time_t _start;
time_t _stop;
};
int main(int argc, const char * argv[])
{
Timer timer;
std::vector<std::thread> workers;
for (int i = 0; i < 15; i++) {
workers.push_back(std::thread([&]()
{
if ( i%2 )
timer.start();
else if (!( i%2 ) )
timer.stop();
if ( i%3 )
timer.reset();
}));
workers[workers.size()-1].join();
}
timer.start();
this_thread::sleep_for(std::chrono::seconds(5));
timer.stop();
std::cout << "Hello, World!\n";
return 0;
}
class Key
{
public:
Key()
{
}
static int zero,two,three;
static int four,five,six,seven,eight,nine;
static int get( char ch )
{
if ( ( ch == 'a' ) || ( ch == 'b' ) || ( ch == 'c' ) )
{
return Key::two;
}
if ( ( ch == 'd' ) || ( ch == 'e' ) || ( ch == 'f' ) )
{
return Key::three;
}
if ( ( ch == 'g' ) || ( ch == 'h' ) || ( ch == 'i' ) )
{
return Key::four;
}
if ( ( ch == 'j' ) || ( ch == 'k' ) || ( ch == 'l' ) )
{
return Key::five;
}
if ( ( ch == 'm' ) || ( ch == 'n' ) || ( ch == 'o' ) )
{
return Key::six;
}
if ( ( ch == 'p' ) || ( ch == 'q' ) || ( ch == 'r' ) || (ch == 's'))
{
return Key::seven;
}
if ( ( ch == 't' ) || ( ch == 'u' ) || ( ch == 'v' ) )
{
return Key::eight;
}
if ( ( ch == 'w' ) || ( ch == 'x' ) || ( ch == 'y' ) || (ch == 'z'))
{
return Key::nine;
}
return Key::zero;
}
};
int Key::zero = 0;
int Key::two = 2;
int Key::three = 3;
int Key::four = 4;
int Key::five = 5;
int Key::six = 6;
int Key::seven = 7;
int Key::eight = 8;
int Key::nine = 9;
class T9dict
{
vector<unordered_set<string>> _dict;
public:
T9dict( vector<string>& words )
{
_dict.resize(10);
addWords(words);
}
void addWords( vector<string>& words )
{
for (string w: words)
{
if ( w.empty() )
continue;
_dict[Key::get(w[0])].insert(w);
}
}
const unordered_set<string>& lookup( int key )
{
return _dict[Key::get(key)];
}
};
vector<vector<int> > generateMatrix(int A)
{
vector<vector<int>> ans(A,vector<int>(A));
int dir = 0;
int up = 0;
int down = A;
int left = 0;
int right = A;
int paint = 1;
int end = A*A;
while ( paint <= end )
{
if ( dir == 0 )//left to right
{
for ( int i = left; i < right; i++ )
{
ans[up][i] = paint++;
}
dir = 1;
print(ans);
continue;
}
if ( dir == 1 )//top to down
{
for ( int i = up+1; i < down; i++ )
{
ans[i][right-1] = paint++;
}
dir = 2;
print(ans);
continue;
}
if ( dir == 2 )//right to left
{
for ( int i = right-2; i > left-1; i-- )
{
ans[down-1][i] = paint++;
}
dir = 3;
print(ans);
continue;
}
print(ans);
if ( dir == 3 )//bottom up
{
for ( int i = down-2; i > up; i-- )
{
ans[i][left] = paint++;
}
up++;
left++;
right--;
down--;
dir = 0;
}
print(ans);
}
return ans;
}
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode * reverseK(ListNode * cur)
{
ListNode * prev=0;
ListNode * next=0;
while ( cur )
{
next=cur->next;
cur->next=prev;
prev=cur;
cur=next;
}
return prev;
}
ListNode * rotateK(ListNode * head, int k)
{
ListNode * l1 = head;
ListNode * prev = 0;
while(k--)
{
prev=head;
head=head->next;
}
if ( head == 0 )
return l1;
if (prev) prev->next=0;
ListNode* l2=head;
l2=reverseK(l2);
if ( k ) head->next=l1;
return l2;
}
string sumv( vector<vector<int>> &ans)
{
vector<int> res;
int pos = 0;
int carry=0;
for ( int col = 0; col < ans[ans.size()-1].size(); col++ )
{
int sum = carry;
for ( vector<int>& row: ans )
{
if ( col < row.size() )
sum+=row[col];
}
if ( col == res.size() )
res.push_back(0);
else
sum+=res[col];
if ( sum > 9 )
{
carry=sum/10;
sum=sum%10;
}
else
carry=0;
res[col] = sum;
}
string str;
for_each(res.rbegin(), res.rend(),[&](int & i)
{
str.append(1,(i+'0'));
});
return str;
}
void Product(string s1, string s2)
{
int offset=0;
vector<vector<int>> ans;
for ( char cd: s2)
{
vector<int> prod;
for ( int i=0; i <offset;i++)
prod.push_back(0);
int carry = 0;
for ( char cc: s1)
{
int pr = ((cd-'0')*(cc-'0'))+carry;
if (( pr ) > 9 )
{
prod.push_back(pr%10);
carry=pr/10;
}
else
prod.push_back(pr);
}
if ( carry )
prod.push_back(carry);
for_each(prod.begin(), prod.end(),[](int i ) { cout << i <<" ";} );
cout<<endl;
ans.push_back(prod);
offset++;
}
string res = sumv(ans);
unsigned long long verylong = ((unsigned long long)(atoll(s1.c_str())*atoll(s2.c_str())));
cout << res << " " << verylong << endl;
}
int getLongest( string & orig, int start, set<string>& ans)
{
int consec = 1;
for ( int i=start; i < orig.length() -1 ; i++ )
{
if ( orig[i+1] == orig[i]+1 )
{
consec++;
if ( consec > 1 )
ans.insert(orig.substr(start,consec));
}
else
break;
}
return consec;
}
set<string> consecutiveChars(string orig)
{
set<string> consecutive;
int start=0;
while( start < orig.size() )
{
start+=getLongest(orig, start,consecutive);
}
return consecutive;
}
class PhoneBook
{
shared_timed_mutex write;
typedef unordered_map<string, std::shared_ptr<Person>> PersonMap;
PersonMap _map;
public:
PhoneBook()
{
}
void addPerson(const Person & p)
{
lock_guard<shared_timed_mutex> w(write);
shared_ptr<Person> s (new Person(p));
_map[p.name] = s;
_map[p.phone] = s;
}
string lookupByPhone(string phone)
{
return lookup(phone).name;
}
string lookupByName(string name)
{
return lookup(name).phone;
}
private:
Person lookup(string key)
{
shared_lock<shared_timed_mutex> r(write);
PersonMap::iterator i = _map.find( key );
if (_map.end() == i) {
Person pp;
return pp;
}
return *(*i).second.get();
}
};
- drolmal April 18, 2017