This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <algorithm> | |
#include <map> | |
#include <cassert> | |
namespace detail { | |
std::map<char, unsigned int> numerals = { {'I', 1}, {'E', 3}, {'V', 5}, {'X', 10}, {'L', 50} }; | |
unsigned int numeral_rank(char c) { | |
return numerals[c]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <utility> | |
#include <tuple> | |
#include <cassert> | |
struct type_erasure { }; | |
template<class T> | |
struct wrapper : type_erasure { | |
wrapper(T&& w) : w_(std::forward<T>(w)) { } | |
T&& w_; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <bitset> | |
#include <vector> | |
#include <algorithm> | |
template<class T> | |
T Rotate(T i) { | |
return (i << 1) | (i >> sizeof(i)); | |
//return ((i & (1 << (sizeof(T) - 1)) >> sizeof(T)) | (i << 1)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstring> | |
#define TO_STRING_FOR_CLASS(X)\ | |
template<class T>\ | |
const char* ToString();\ | |
\ | |
template<>\ | |
const char* ToString<X>() {\ | |
static const char* str = #X;\ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <new> | |
struct Parent { }; | |
struct Child : Parent { }; | |
struct Tag { }; | |
int main() | |
{ | |
const Child c; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Parent { virtual ~Parent(); }; | |
struct Child : private virtual Parent { }; | |
int main() | |
{ | |
Child aa; | |
Parent& pp = (Parent&)(aa); // OK ( [expr.cast]/p4.6 ), chooses static_cast | |
Child& aa = (Child&)(pp) // FAILS, chooses static_cast, but that would fail | |
// because Parent is a virtual base ( [expr.static.cast]/p2 ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Base { | |
Base(int) {} | |
}; | |
struct D1 : virtual Base { | |
using Base::Base; | |
}; | |
int main() { | |
static_assert(std::is_constructible<D1, int>::value, ""); // FAILS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a string and a non-empty substring sub, compute recursively the largest substring which starts and ends with sub and return its length. */ | |
public int strDist(String str, String sub) { | |
if (str.length() < sub.length()) return 0; | |
if (str.substring(0, sub.length()).equals(sub) && | |
str.substring(str.length() - sub.length(), str.length()).equals(sub)) | |
return str.length(); | |
int p1 = strDist(str.substring(1), sub); | |
int p2 = strDist(str.substring(0, str.length() - 1), sub); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Given a string and a non-empty substring sub, compute recursively | |
the number of times that sub appears in the string, without the sub | |
strings overlapping. */ | |
public int strCount(String str, String sub) { | |
if (str.length() < sub.length()) return 0; | |
if (str.substring(0, sub.length()).equals(sub) && | |
!str.substring(sub.length(), sub.length()).equals(sub)) | |
{ | |
return 1 + strCount(str.substring(sub.length()), sub); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int countHi(String str) { | |
if (str.isEmpty()) return 0; | |
if (str.length()==1)return 0; | |
return countHi(str.substring(1))+((str.substring(0,2).equals("hi"))?1:0) ; | |
} | |
public boolean array220(int[] nums, int index) { | |
if (index >= nums.length-1) return false; | |
if (nums.length <= 1) return false; | |