1475 - I2P(II)2018_Yang_hw_p3 Scoreboard

Time

2018/06/01 17:00:00 2018/06/22 12:00:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
10576 Move
10673 Queueing
10861 Parentheses Matching
11028 swapsort
11447 Use std::map
11485 Use std::set
11498 Count the Leaves
11945 Treasure Hunting
11947 Step by step

10576 - Move   

Description

There are N numbers in a queue.  The origin sequence is from 1 to N. (1 is the head). The operation “Move a b” means to move all the numbers in front of a (including a) and then insert them in front of b without changing order.  Given several such operations and output the final status of the sequence.

Input

There will be only one test case. The test case begins with an integer N indicating the number of people.  There are several operations followed.  The format is “Move a b” (without quote) you can assume that the position of a is in front of the position of b.  The test case is terminated by “Exit” (without quote).

subtask 1 : 1<=N<=100, you can use O(N) algo for each operation.

subtask 2 : 1<=N<=100, you can use O(N) algo for each operation.

subtask 3 : 1<=N<=100000, you need use O(1) algo for each operation.

subtask 4 : 1<=N<=1000000, you need use O(1) algo for each operation.

Output

Output the numbers from the first one to the last one, and separate two consecutive numbers by a space.

Sample Input  Download

Sample Output  Download

Tags

qq 好難 類似linked list 為何我過後兩個但沒過前兩個== 而且前兩的是TLE output最後要有換行 幫QQ Jinkela 為什麼這個開放給大家隨便亂加啊? 因為給大家期末抒發的管道



Discuss




10673 - Queueing   

Description

You need to write a program to simulate a queue of names.  Each name is a string consisting of English letters only.  There are three operations:

1.         “Push [name]”, which means to enque name in the queue.

2.         “Pop”, which means to deque. If the queue is empty, this operation takes no effect.

3.         “Front”, which means to print out the name in the front of queue. If the queue is empty, print "empty" (without quotes).

 

Case1 : #operation <= 10^2, There will be no "Pop" and "Front" command when queue is empty.
Case2 : #operation <= 10^3. There will be no "Pop" and "Front" command when queue is empty.
Case3 : #operation <= 10^4.
Case4 : #operation <= 10^6.

Input

Each line contains one of the following operations. “Push [name]” (without quotes), “Pop” (without quotes), “Front”(without quotes). The length of each name is at most 10.

Output

For each “Front” operation, print out the name in the front of the queue.

Sample Input  Download

Sample Output  Download

Tags




Discuss




10861 - Parentheses Matching   

Description

A string is said to be valid if it matches one of the following rules:

(1) The string is an empty string.

(2) If a string S is valid, then {S}, [S], (S) and <S> are valid.

(3) If strings S1 and S2 are both valid, then S1S2 is valid.

Given a string consisting of parentheses, determine if it is a valid string.

Input

The first line of the input contains an integer N (N ≤ 1000) denoting the number of test cases followed by. Each of the next N lines corresponds to a test case, which contains a string consisting of parentheses, and the maximum string length will be no more than 1000. Note that an empty string (a line which contains the newline character only) may be contained in the input and it should be considered as a valid string according to rule (1).

For case 1,2 : 1<N<100. 0<=length<100

For case 3,4 : 1<N<1000. 0<=length<1000

Output

For each test case, print “Case i:” and then “Yes” or “No” to indicate that the string is valid or not, separated by a space character. i is the test case number starting from 1.

Sample Input  Download

Sample Output  Download

Tags




Discuss




11028 - swapsort   

Description

Given an integer sequence, say {6, 1, 3, 4, 5, 2}, can we sort the sequence by keeping swapping the first number with its two neighbors: the second number and the last number ? 

For example, Swap the first number 6 and with the last number 2 to get {2, 1, 3, 4, 5, 6}, and then swap the first number 2 and the second number 1 to get {1, 2, 3, 4, 5, 6}, and we get the sequence sorted.

In function.h we define a class called SwapSort. A constructor and a member function show_solutions are implemented. Your task is to complete the implementation of the following two functions:

1. set<State> extend(State s);
A State is defined as using State = vector<int>;
For some State s, we may extend it by swapping the first number with the second one, or swapping the first number with the last one. Therefore, from State s we may generate two new states, and insert them into a set.

2. void solve(int steps);
This function tries to solve the problem in a number of swaps specified by steps. The found solutions are stored in the class member set<list<State>> _solutions;

Notice that, no duplicate states are allowed in a solution.

You need to implement the two functions in function.cpp

main.cpp

function.h

function.cpp

Input

An integer sequence, separated by spaces. End by EOF.

Output

The output will be generated by function.h

Sample Input  Download

Sample Output  Download

Partial Judge Code

11028.cpp

Partial Judge Header

11028.h

Tags

10402HW10



Discuss




11447 - Use std::map   

Description

This problem will help you understand how to use std::map.

http://en.cppreference.com/w/cpp/container/map

Notice, this time, first and last is [first, last].

目前有五個測資,第三筆測資不算,答題測資看另外四筆

Input

The input consist of series of command. Each commands is insert, range output or range erase.

insert: inserts a string (cmd) with the key (key) to map. If the key has already existed, insert the cmd at the begining of string (the string which the key belongs).

For example,

insert 0 "abc"

insert 1 "def"

the map should contain "abc", "def".

insert 0 "xyz"

the map should contain "xyzabc", "def".

 

range output: outputs the string from key (first) to key (last). Output a space character (' ') after printing an element.

 

range erase: erase the string from key (first) to key (last).

 

operator<<: outputs all strings in map. From the smallest key to biggest key. Output a space character (' ') after printing an element.

Output

Complete insert, output, erase and operator<<.

Sample Input  Download

Sample Output  Download

Partial Judge Code

11447.cpp

Partial Judge Header

11447.h

Tags




Discuss




11485 - Use std::set   

Description

This problem will help you understand how to use std::set and comparator.

e.g., std::set<std::vector<int>>

http://en.cppreference.com/w/cpp/container/set

Input

How to compare the integer sequence (calculate the key of an integer sequence):

  • get the length of integer sequence (size)
  • key value=[first element]*(size)+[second element]*(size-1)+[third element]*(size-2)+...+[last element]*1
  • compare the key value. Small key value is smaller.
  • if the length of a integer sequence is 0, the key value is 0.
  • e.g., the key of an integer sequence "3 9 -1 3 -6" is 48 (3*5+9*4+(-1)*3+3*2+(-6)*1)

 

The input consist of series of command. Each commands is insert, range_out or output.

​insert: inserts an integer sequence (each value is greater than -6 and smaller than 11, stop reading from cin when value is 0). If the key has already existed, then

  • output "exist\n"
  • erase the integer sequence from set
  • reverse the integer sequence (from input)
  • insert the reversed integer sequence

For example,

insert 3 9 -1 3 -6 0

The set should contain 3 9 -1 3 -6.

insert 9 -2 6 6 0

The set should contain 6 6 -2 9.


range_out: outputs all integer sequences from the integer sequence (first key) to the integer sequence (second key) of set (including the second key). First key and second key are read from input (each value is greater than -6 and smaller than 11, stop reading from cin when value is 0).

For example,

insert 2 -5 -5 0

insert 3 9 -1 3 -6 0

insert 7 6 1 2 0

insert 10 10 10 2 0

range_out -5 0 10 9 2 0

It should output

3 9 -1 3 -6

7 6 1 2

For example,

insert 2 -5 -5 0

insert 3 9 -1 3 -6 0

insert 7 6 1 2 0

insert 10 10 10 2 0

range_out -5 0 10 10 10 0

It should output

3 9 -1 3 -6

7 6 1 2

 

output: outputs all integer sequences from the smallest key to the biggest key of set. Output a space character (' ') after printing an integer. Output a newline character ('\n') after printing all elements of a key.

Output

Complete insert, range output and output.

Sample Input  Download

Sample Output  Download

Tags




Discuss




11498 - Count the Leaves   

Description

Given a tree, count the number of leaves in this tree. Each node has unique integer identification (ID), but all IDs may not be consecutive.

Input

There are multiple test cases. Each test case begins with an integer N (1 <= N <=1000). In the following N lines, each line has two integer a and b (1 <= a,b <= 1000), indicating node a is node b’s parent. The next line contains an integer R, which represents the root of the Tree. You can assume that all the nodes will be on the same tree. The input is terminated by N = 0.

Output

Print the number of the leaves for each test case.

Hint: All leaves have no children!

Sample Input  Download

Sample Output  Download

Tags




Discuss




11945 - Treasure Hunting   

Description

Niflheimr is playing Minecraft recently. He occupies a lot of towns, and there are many diamonds inside some of those towns.

Whenever he runs out of diamond, he has to figure out the nearest town with diamonds from his current location.

Doing this task every time waste a lot of time. He asks you to help him for the calculation. For every town, find the shortest distance to get diamonds from a nearby town which has diamonds. If the town itself has diamonds, the distance would be 0.

Hint: Use BFS (Breadth First Search) and std::queue.

If you are not familiar with BFS, you should take a look at the following article:

http://alrightchiu.github.io/SecondRound/graph-breadth-first-searchbfsguang-du-you-xian-sou-xun.html

[IMPORTANT] For those almost run out of time, maybe this note can help you figure out BFS and solve this problem faster!


You are provided with the following code:

#include <queue>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// G[i] is the neighbor towns of town i
vector<int> diamondTowns;
vector<int> G[100005];
int Dist[100005];

struct node {
    int id;
    int dist;
    node(int id, int l) {
        this->id = id;
        this->dist = l;
    }
};

int main() {
    int N, M, K;
    cin >> N >> M >> K;
    for (int i = 0; i < M; i++) {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    for (int i = 0; i < K; i++) {
        int x;
        cin >> x;
        diamondTowns.push_back(x);
    }
    fill(Dist, Dist+100005, -1);

    queue<node> Q;

    // [TODO] complete the task!

    // Output
    for (int i = 1; i <= N; i++) {
        cout << Dist[i] << '\n';
    }
    return 0;
}

 You may take it as reference. You are not required to use this code, though.

Input

First line of input contains three integer N, M, K, denoting the number of towns, the amount of roads between towns and the number of towns which has diamond.

All the towns are numbered from 1 to N.

For the next M lines, each of them contains two integer a, b, meaning there is a road between a and b. All the roads are bidirectional, and the length of these roads are all 1.

The last line of input contains K distinct integers, denoting the towns which has diamonds.

It is guaranteed that:

  • 1 <= K <= N <= 105
  • 1 <= M <= min(106, N*(N+1)/2)
  • All the towns can reach each other, i.e. the graph is connected.

 

Output

Output consists of N lines.

For i-th line, output the shortest distance Niflheimr has to travel from town i to get a diamond.

Sample Input  Download

Sample Output  Download

Tags




Discuss




11947 - Step by step   

Description

Niflheimr creates a special robot, which can only take steps with some special length.

Now given lengths of those steps and the target location, please tell Niflheimr the minimum steps the robot has to take to reach the target. The robot can choose different step lengths each time while taking a new step.

The robot is located at 0 initially. Note that the robot can go backward.

Hint: Try to memorize locations that has been computed before. std::queue and std::set may be useful.

Input

First line of input contains an integer T, denotng the number of testcases.

Each testcases consists of two lines:

First line of each testcase contains two integer N, D, denoting the number of steps the robot can take and the target location, respectively.

Second line of each testcase contains N distinct integers, denoting the lengths of steps the robot can take.

It is guaranteed that:

  • 1 <= T <= 5
  • 1 <= N <= 10
  • 1 <= D <= 1000
  • 1 <= length of each step <= 2000

 

Output

For each testcase, print one integer x in a single line, representing the minimum steps the robot has to take to reach the target. If the robot can't reach the target, print -1.

Sample Input  Download

Sample Output  Download

Tags




Discuss