Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Careercup cpp 20190409 1232 1 #10

Merged
merged 3 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions careercup/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

# Overview

Some Careercup Related Exercises and Solutions

# Composition

- Words with N Dices

> Given a word of length n, and n six-sided dice with a character in each side, find out if this word is constructible by the set of given dice

- [Sol](words_with_n_dices1.cpp)









Work in progress


60 changes: 60 additions & 0 deletions careercup/words_with_n_dices1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @brief Solution the words with n dices
*
* Description
*
* Given a word of length n, and n six-sided dice with a character in each side, find out if this word is constructible by the set of given dice
*/

#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;

class Dice
{
public:
Dice(const vector<char>& v)
{
for(const auto& c : v) db.insert(c);
}

bool is_member(const char c) const { return (db.find(c) != db.end()); }

private:
unordered_set<char> db;
};



bool solve( const vector<Dice>& v, const unordered_set<unsigned int>& exclude_list, const string& s )
{
if(s=="") return true;
for(unsigned int i=0; i<v.size(); ++i)
{
if(exclude_list.find(i) != exclude_list.end()) continue;
const auto d = v[i];
const auto c = s[0];
if(d.is_member(c))
{
auto temp = exclude_list;
temp.insert(i);
if( solve( v, temp, s.substr(1) ) ) return true;
}

}
return false;
}

string show(const bool b) { return (b?"True":"False"); }

int main() {
// your code goes here
vector<Dice> v( {Dice({'a', 'b', 'c'}), Dice({'a', 'd', 'e'}), Dice({'g', 'l', 'm'}) } );
vector<string> tests = {"adm", "ekg", "ecg"};
for(const auto& t : tests) cout << show(solve(v, {}, t)) << endl;

return 0;
}