| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 10632 | Mid2_Exam Palindrome |
|
| 10633 | Mid2_Exam Queue using polymorphism |
|
| 10667 | Block 3 |
|
| 10668 | Appendix for 10667 |
|
Description
Palindrome is a string that is identical to its reverse, like "level" or "aba". Check whether a given string is a palindrome or not.
// function.h
#include <iostream>
#include <string>
using namespace std;
class PalindromeChecker
{
private:
static const int MAXWORDLENTH=100001;
char word[MAXWORDLENTH];//store the input string
public:
PalindromeChecker();
PalindromeChecker(const char*);
PalindromeChecker& operator=(const PalindromeChecker& p);
void reverse();
friend bool operator== (PalindromeChecker &p1, PalindromeChecker &p2);
};
main.cpp
// main.cpp
#include <iostream>
#include "function.h"
using namespace std;
int main()
{
char word[100001];
while(cin >> word){
PalindromeChecker p1(word);
PalindromeChecker p2;
p2 = p1;
p2.reverse();
if(p1==p2)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
Input
The input consists of multiple lines. Each line contains a string. The length of each string is less than 100000. The number of test case is less than 1000.
Output
For each test case, output "Yes" if it's a palindrome or "No" if it's not a palindrome in a line.
Sample Input Download
Sample Output Download
Partial Judge Code
10632.cppPartial Judge Header
10632.hTags
Discuss
Description
A queue is an abstract data type that serves as a collection of elements, where nodes are removed only from the head of the queue and are inserted only at the tail of the queue. Two principal operations can be used to manipulate a queue: enqueue, which inserts an element at the tail, and dequeue, which removes the element at the head of the collection.
Let’s see how the queue data structure can be realized in C++.We have an approach to implement queue: linked list. Thus, we define two classes as follows:
class Queue{
friend std::ostream &operator<<(std::ostream &, Queue &);
public:
virtual ~Queue() {};
virtual void enqueue(const int &) = 0;
virtual int dequeue() = 0;
virtual void print(std::ostream &output)=0;
};
class List_queue : public Queue{
public:
List_queue();
virtual ~List_queue();
void enqueue(const int &);
int dequeue();
void print(std::ostream &output);
private:
ListNode *head;
ListNode *tail;
};
where
- Class Queue serves as the abstract base class for realizing polymorphism
- List_queue implements the queue data structure
Besides, we also overload the stream insertion operator (<<) to print the content of a queue object polymorphically.
REQUIREMENTS:
- Implement the enqueue(), dequeue() and print() member functions of the List_queue class.
- Implement the overloaded stream insertion operator (<<), which will call the correct member function print() polymorphically.
Note:
1.This problem involves three files.
- function.h: Class definitions.
- function.cpp: Member-function definitions.
- main.cpp: A driver program to test your class implementation.
You will be provided with main.cpp and function.h, and asked to implement function.cpp.
<code>function.h</code>
#ifndef FUNCTION_H
#define FUNCTION_H
#include <iostream>
class ListNode
{
friend class List_queue; //make List_queue a friend
public:
ListNode( const int &info ) //constructor
: data( info ), nextPtr( NULL ), prevPtr( NULL )
{
} //end ListNode constructor
private:
int data; //data
ListNode *nextPtr; // next node in list
ListNode *prevPtr;
}; //end class ListNode
class Queue{
friend std::ostream &operator<<(std::ostream &, Queue &);
public:
virtual ~Queue() {};
virtual void enqueue(const int &) = 0;
virtual int dequeue() = 0;
virtual void print(std::ostream &output)=0;
};
class List_queue : public Queue{
public:
List_queue();
virtual ~List_queue();
void enqueue(const int &);
int dequeue();
void print(std::ostream &output);
private:
ListNode *head;
ListNode *tail;
};
#endif // FUNCTION_H
<code>main.cpp</code>
#include <iostream>
#include <string.h>
#include "function.h"
using namespace std;
int main(){
List_queue L_queue;
char command[10];
int n;
while(cin>>command){
if(strcmp(command,"dequeue")==0){
L_queue.dequeue();
}else if(strcmp(command,"enqueue")==0){
cin >> n;
L_queue.enqueue(n);
}else if(strcmp(command, "print") == 0){
cout << L_queue << endl;
}
}
return 0;
}
2.For OJ submission:
Step 1. Submit only your function.cpp into the submission block.
Step 2. Check the results and debug your program if necessary.
Input
There are three kinds of commands:
-
“enqueue integerA” represents inserting an element with int value A at the tail of the queue.
-
“dequeue” represents removing the element at the head of the queue.
-
“print” represents showing the current content of the queue.
Each command is followed by a new line character.
Input terminated by EOF.
Output
The output should consist of the current state of the queue.
When the queue is empty, you don’t need to print anything except a new line character.
Sample Input Download
Sample Output Download
Partial Judge Code
10633.cppPartial Judge Header
10633.hTags
Discuss
Description
In function.h we define a class called Block. Several constructors and member functions are implemented. Your task is to complete the implementation of the following three functions and apply them to solva one problem:
1. void clockwise90();
This function rotates the pattern by 90 degrees clockwise. For example,
@OOX
OOXX
XXXX
XXXX
becomes
XXO@
XXOO
XXXO
XXXX
2. Block& doublesize();
This function enlarges the pattern to make it twice as wide and twice as tall. For example,
CCCC
OOOO
OOOO
LLLL
becomes
CCCCCCCC
CCCCCCCC
OOOOOOOO
OOOOOOOO
OOOOOOOO
OOOOOOOO
LLLLLLLL
LLLLLLLL
3. friend bool equal(const Block& a, const Block& b);
This function checks if two blocks a and b have the same pattern under rotation. You need to take into consideration the four orientations. For example,
1.
@OOX
OOXX
XXXX
XXXX
and
XXXX
XXXX
XXOO
XOO@
are EQUAL
2.
@OOX
OOXX
XXXX
XXXX
and
XOO@
OOXX
XXXX
XXXX
are DIFFERENT
3.
XXXX
XXXX
XXXX
XXXX
and
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
are DIFFERENT.
You need to implement the three functions in function.cpp
Finally, you need to use equal() to check two blocks and output the result.
Input
The input will be given in main.cpp
Output
The output will be generated by main.cpp
Sample Input Download
Sample Output Download
Partial Judge Code
10667.cppPartial Judge Header
10667.hTags
Discuss
Description
code of function.h
#ifndef _BLOCKCLASS_
#define _BLOCKCLASS_
#include <iostream>
#include <algorithm>
#include <utility>
class Block {
private:
int size;
char** pattern; // array of pointers to buf
char* buf; // 2D pattern stored in 1D raster scan order
public:
Block(): size{0}, pattern{nullptr}, buf{nullptr} {}
Block(int sz, const char* pat):
size{sz}, pattern{new char* [size]}, buf{new char[size*size]}
{
// std::cout << "custom constructor\n";
for (int i=0; i<size*size; i++) buf[i]=pat[i];
for (int i=0; i<size; i++) {
pattern[i] = (char*) &buf[i*size];
}
}
Block(const Block &b):
size{b.size}, pattern{new char* [size]}, buf{new char[size*size]}
{
// std::cout << "copy constructor\n";
for (int i=0; i<size*size; i++) buf[i]=b.buf[i];
for (int i=0; i<size; i++) {
pattern[i] = (char*) &buf[i*size];
}
}
Block& operator=(Block& c) {
Block b{c};
// std::cout << "copy assignment\n";
std::swap(buf, b.buf);
std::swap(pattern, b.pattern);
size = b.size;
return *this;
}
// rvalue reference
Block(Block&& b): size{b.size}, pattern{b.pattern}, buf{b.buf}
{
// std::cout << "move constructor\n";
b.size = 0;
b.pattern = nullptr;
b.buf = nullptr;
}
// rvalue reference
Block& operator=(Block&& b) {
// std::cout << "move assignment\n";
if (this != &b) {
delete [] buf;
delete [] pattern;
buf = b.buf;
pattern = b.pattern;
size = b.size;
b.buf = nullptr;
b.pattern = nullptr;
b.size = 0;
}
return *this;
}
~Block()
{
// std::cout << "destructor\n";
delete [] buf;
delete [] pattern;
}
friend std::ostream& operator<<(std::ostream& os, Block& b) {
for (int i=0; i<b.size; i++) {
for (int j=0; j<b.size; j++) {
os << b.pattern[i][j];
}
os << std::endl;
}
return os;
}
// the task is to implement the following three functions
void clockwise90();
Block& doublesize();
friend bool equal(const Block& a, const Block& b);
};
#endif
sample code of main.cpp
#include "function.h"
#include <iostream>
int main()
{
Block b;
b= Block{4, "XXXX" "XXXX" "XXXX" "XXXX"};
Block c = b;
c.doublesize();
if (equal(b, c))
std::cout << "EQUAL" << std::endl;
else
std::cout << "DIFFERENT" << std::endl;
Block d;
d= Block{4, "CCCC" "OOOO" "OOOO" "LLLL"};
Block e = d;
e.doublesize();
e.clockwise90();
d.doublesize();
if (equal(d, e))
std::cout << "EQUAL" << std::endl;
else
std::cout << "DIFFERENT" << std::endl;
}