770 - I2P(II)2015_Lee_Mid2 Scoreboard

Time

2015/05/27 08:10:00 2015/05/27 09:50:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
10632 Mid2_Exam Palindrome
10633 Mid2_Exam Queue using polymorphism
10634 Rotation Invariant
10635 Appendix for 10634

10632 - Mid2_Exam Palindrome   

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.cpp

Partial Judge Header

10632.h

Tags




Discuss




10633 - Mid2_Exam Queue using polymorphism   

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

  1. Class Queue serves as the abstract base class for realizing polymorphism
  2. 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:

  1. Implement the enqueue(), dequeue() and print() member functions of the List_queue class.
  2. 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.cpp

Partial Judge Header

10633.h

Tags




Discuss




10634 - Rotation Invariant   

Description

In function.h we define a class called Block. If a Block is rotation invariant, it is always displayed in the same form no matter how many times it is rotated. For example,

@OOX
OOXX
XXXX
XXXX

is not rotation invariant, but

@OO@
OOOO
OOOO
@OO@

is rotation invariant.

Your task is to complete the implementation of clockwise90(), which rotates the pattern by 90 degrees clockwise, first. Then invariant() to check if the input Block is rotation invariant or not.

The following is the format of invariant() and clockwise(), respectively.

friend bool invariant(const Block& a);

void clockwise90();

You need to implement these functions in function.cpp (main.cpp and function.h are provided in10635).

NOTICE: If you get Judge Error, you may change the compile setting to c++11 while submitting your code.

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

10634.cpp

Partial Judge Header

10634.h

Tags




Discuss




10635 - Appendix for 10634   

Description

sample code of main.cpp

#include <iostream>
#include "function.h"

int main()
{
    Block b{4, "@OO@" "OOOO" "OOOO" "@OO@"}; // create a 4x4 pattern, which is rotation invariant
    /*std::cout << b << std::endl;*/

    if (invariant(b)) // checking if two patterns are equivalent under rotation
        std::cout << "INVARIANT" << std::endl;
    else
        std::cout << "VARIANT" << std::endl;

    Block c{4, "@XX@" "OOOO" "OOOO" "@OO@"}; // create a 4x4 pattern, which is rotation variant
    /*std::cout << c << std::endl;*/

    if (invariant(c)) // checking if two patterns are equivalent under rotation
        std::cout << "INVARIANT" << std::endl;
    else
        std::cout << "VARIANT" << std::endl;

}

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 two functions
    void clockwise90();
    friend bool invariant(const Block& a);

};
#endif

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss