| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11459 | Cheat Sheet |
|
| 11933 | Vector Dot |
|
| 11935 | double-end-queue |
|
| 11938 | Another Encoding and Decoding |
|
Description
Array.cpp
// overloaded assignment operator;
// const return avoids: ( a1 = a2 ) = a3
const Array &Array::operator=( const Array &right )
{
if ( &right != this ) // avoid self-assignment
{
// for Arrays of different sizes, deallocate original
// left-side Array, then allocate new left-side Array
if ( size != right.size )
{
delete [] ptr; // release space
size = right.size; // resize this object
ptr = new int[ size ]; // create space for Array copy
} // end inner if
for ( size_t i = 0; i < size; ++i )
ptr[ i ] = right.ptr[ i ]; // copy array into object
} // end outer if
return *this; // enables x = y = z, for example
} // end function operator=
// determine if two Arrays are equal and
// return true, otherwise return false
bool Array::operator==( const Array &right ) const
{
if ( size != right.size )
return false; // arrays of different number of elements
for ( size_t i = 0; i < size; ++i )
if ( ptr[ i ] != right.ptr[ i ] )
return false; // Array contents are not equal
return true; // Arrays are equal
} // end function operator==
// overloaded subscript operator for non-const Arrays;
// reference return creates a modifiable lvalue
int &Array::operator[]( int subscript )
{
// check for subscript out-of-range error
if ( subscript < 0 || subscript >= size )
throw out_of_range( "Subscript out of range" );
return ptr[ subscript ]; // reference return
} // end function operator[]
// overloaded subscript operator for const Arrays
// const reference return creates an rvalue
int Array::operator[]( int subscript ) const
{
// check for subscript out-of-range error
if ( subscript < 0 || subscript >= size )
throw out_of_range( "Subscript out of range" );
return ptr[ subscript ]; // returns copy of this element
} // end function operator[]
// overloaded input operator for class Array;
// inputs values for entire Array
istream &operator>>( istream &input, Array &a )
{
for ( size_t i = 0; i < a.size; ++i )
input >> a.ptr[ i ];
return input; // enables cin >> x >> y;
} // end function
// overloaded output operator for class Array
ostream &operator<<( ostream &output, const Array &a )
{
// output private ptr-based array
for ( size_t i = 0; i < a.size; ++i )
{
output << setw( 12 ) << a.ptr[ i ];
if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
output << endl;
} // end for
if ( a.size % 4 != 0 ) // end last line of output
output << endl;
return output; // enables cout << x << y;
} // end function operator<<
|
Inheritance type |
Public |
Protected |
Private |
|
Public data/ functions in the base class |
Public in the derived class |
Protected in the derived class |
Private in the derived class |
|
Protected data/ functions in the base class |
Protected in the derived class |
Protected in the derived class |
Private in the derived class |
|
Private data/fun in the base class |
Not accessible in the derived class |
Not accessible in the derived class |
Not accessible in the derived clas |
Polymorphism
In the base class
virtual void foo();
In the derived class
virtual void foo() override;
Abstract class and pure virtual function
virtual void foo() =0;
Prefix/postfix operators
complex &operator++(); // prefix
complex operator++(int); // postfix
Operator overloading
Binary operators
/*1.non-static member function */
complex operator+ (const complex&);
/* 2. non-member function */
friend complex operator* (const
complex&, const complex&);
Unitary operators
/*1.non-static member function */
complex operator-();
/*2. non-member function */
friend complex operator~(const complex&);
Inheritance
class A {
// A is a base class }
class B: public A {
// B inherits A.
// B is a derived class}
Dynamic allocation
int *ptr = new int;
delete ptr;
int *ptr = new int[100];
delete [] ptr;
String object
#include<string>
using namespace std;
...
string str1 = "Hello";
string str2 = "World";
Standard C++ Library - <string>
|
operator[] |
For string a, a[pos]: Return a reference to the character at position pos in the string a. |
|
operator+= |
Append additional characters at the end of current string. |
|
operator+ |
Concatenate strings. |
|
operator< |
Compare string values. For string a and b, a < b: if a is in front of b in dictionary order, return 1, else return 0. |
|
operator> |
Compare string values. For string a and b, a > b: if a is in back of b in dictionary order, return 1, else return 0. |
|
operator== |
Compare string values. For string a and b, a == b: if equal, return 1, else return 0; |
|
operator!= |
Compare string values. For string a and string b, a != b: if not equal, return 1, else return 0; |
|
compare() |
Compare string. For string a and b, a.compare(b): if equal, return 0. If string a is in front of string b in dictionary order, return -1. If string a is in back of string b in dictionary order, return 1. |
|
length() |
Return length of string. |
|
swap() |
Swap string values. |
|
push_back() |
For string a, a.push_back(c): append character c to the end of the string a, increasing its length by one. |
Standard C++ Library - <sstream>
|
operator<< |
Retrieves as many characters as possible into the stream. |
|
operator>> |
Extracts as many characters as possible from the stream. |
|
str |
Returns a string object with a copy of the current contents of the stream. |
For example(1):
stringstream ss{“Hello”}; // constructor
string str = ss.str(); // a = “Hello”
For example(2):
stringstream ss;
ss<<”75”;
ss<<”76”;
int num;
ss>>num; // num = 7576
Input
Output
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Implement a Vector class that support n-dimensional vector dot product.
Your task is to complete
- const int operator[](int index) const;
- int operator*(const Vector& a);
inside the Vector class;
Remember to #include "function.h"!
Input
First line of input is an integer m, where m is the number of testcases.There are m testcases following.
A testcases consists of three lines:
- In the first line of each testcase, there are a string OP and an integer n, where OP is the operation and n is the vector size.
- In the second line of each testcase, there are n integers, representing all the elements inside the first vector, A.
- In the third line of each testcase, there are n integers, representing all the elements inside the second vector, B.
It is guaranteed that:
- 1 ≤ n, m ≤ 100
- For all the elements x in vector A and B, |x| ≤ 100
Output
For each testcase, according to its operation, output the result of A*B. There is a space after every number.
Sample Input Download
Sample Output Download
Partial Judge Code
11933.cppPartial Judge Header
11933.hTags
Discuss
Description
There's a dequeue already define in "function.h" and stack , queue inherit from the _dequeue.
Please implement those function below:
stack::
void push(const _node N);
void pop();
_node* get_data();
queue::
void push(const _node N);
void pop();
_node* get_data();
Good luck.
Input
There will be few instruction in input [cont] [inst] [data].
cont and inst will be string
cont will be "stack" or "queue"
inst will be "push", "pop", "front" for queue only and "top" for stack only.
data will be optional (only when push inst , it will give the data ")
"exit" means exit
Output
In every "get_data"(top or front) commands , it will be single line with the data.
Sample Input Download
Sample Output Download
Partial Judge Code
11935.cppPartial Judge Header
11935.hTags
Discuss
Description
Niflheimr has too many codes in his computer, so he decided to compress some text files to reserve more disk space.

He comes up with two compression method, Run-length and TwoChar encoding.
The rule of Run-length encoding is simple: count the number of consecutive repeated characters in a string, and replace the repeated characters by the count and a single instance of the character. For example, if the input string is 'AAADDDDDDDBBGGGGGCEEEE', its run-length encoding will be '3A7DBB5GC4E', because there are three A's, seven D's, ... etc. Note that we do not need to encode runs of length one or two, since '2B' and '1C' are not shorter than 'BB' and 'C'.
The rule of TwoChar encoding is also simple: given a mapping table which maps TWO Latin character pattern P to ONE non-Latin character token T, then replace all the pattern Pi inside the given string with token Ti. Observe that there might be many ways to encode a string with TwoChar encoding.
Following is an example of TwoChar encoding:
| Pattern | Token |
|---|---|
| AA | * |
| BB | - |
'AABBC' can be encoded into '*-C'.
Now Niflheimr wants you to help him finish the task by implementing
void RleCodec::encode()for Run-length encodingvoid TwoCharCodec::encode()for TwoChar encoding
He will be happy if he can reserve as much space as possible, so for TwoChar encoding, please provide a encoding way which can produce a optimal encoded string, i.e. provide an encoded string with MINIMUM LENGTH possible. He will check the length of your encoded string to judge your solution.
Using the previous table, 'AAAA' should be encoded into '**', 'A*A' is not an optimal encoded string.
For submission, your code may look like the following:
#include "function.h"
// other include if you need
void RleCodec::encode() {
// your code
}
void TwoCharCodec::encode() {
// your code
}
Moreover, choose '-std=c++11' in the compile option while submitting your code to OJ!
In function.h, we add the class DummyCodec as a sample of implementing a derived class of the base class Codec.
Input
First line of input is the string S which will be encoded.
Second line contains an integer N, representing the size of pattern table for TwoChar encoding.
Then for the following N lines, each of them is of the following format:
** -
Where ** is the pattern, and - is the corresponding token.
All the patterns contain only Latin characters, i.e. [A-Za-z], and none of the tokens is Latin characters.
It is guaranteed that all the patterns and tokens are distinct, and the length of the string S is less than 300.
For the first 2 testcases, N == 0. So you can still pass first 2 testcases even if you only implement Run-length encoding!
Output
The first and second lines are RLE encoding and decoding.
The third line prints the length of your TwoChar encoded string. If the length is not optimal, you will get a Wrong Answer.
The fourth line is the result of decoding your TwoChar encoded string. It should be identical to the input string.