首页 > ACM题库 > HDU-杭电 > HDU 1702 ACboy needs your help again![解题报告] C++
2013
12-21

HDU 1702 ACboy needs your help again![解题报告] C++

ACboy needs your help again!

问题描述 :

ACboy was kidnapped!!
he miss his mother very much and is very scare now.You can’t image how dark the room he was put into is, so poor :(.
As a smart ACMer, you want to get ACboy out of the monster’s labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can’t solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem’s first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out").
and the following N lines, each line is "IN M" or "OUT", (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!

输入:

The input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.

输出:

For each command "OUT", you should output a integer depend on the word is "FIFO" or "FILO", or a word "None" if you don’t have any integer.

样例输入:

4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT

样例输出:

1
2
2
1
1
2
None
2
3

题目大意:理解起来很简单,读者可以直接上OJ上面看

代码如下:

/*
 * 1702_1.cpp
 *
 *  Created on: 2013年8月7日
 *      Author: Administrator
 *      为了能有章泽天这样的女朋友而不断努力。。。。
 */

#include <iostream>
#include <stack>
#include <queue>

using namespace std;

int main() {
	int t, m;
	string str;
	cin >> t;
	stack<int> p;
	queue<int> q;
	while (t--) {
		/**
		 * 因为STL中的stack、queue没有专门的clear()函数
		 * 所以每次都要自己写清空stack、queue的函数
		 */
		while(!p.empty()){
			p.pop();
		}

		while(!q.empty()){
			q.pop();
		}
		cin >> m >> str;

		if (str == "FIFO") {

			for (int i = 0; i < m; ++i) {
				string cmd;
				int n;
				cin >> cmd;
				if (cmd == "IN") {
					cin >> n;
					q.push(n);
				} else if (cmd == "OUT") {
					if (!q.empty()) {
						int a = q.front();
						q.pop();
						cout << a << endl;
					} else {
						cout << "None" << endl;
					}

				}
			}
		} else {
			for (int i = 0; i < m; ++i) {
				string cmd;
				int n;
				cin >> cmd;
				if (cmd == "IN") {
					cin >> n;
					p.push(n);
				} else if (cmd == "OUT") {
					if (!p.empty()) {
						int a = p.top();
						p.pop();
						cout << a << endl;
					} else {
						cout << "None" << endl;
					}

				}
			}

		}

	}

}

解题报告转自:http://blog.csdn.net/hjd_love_zzt/article/details/9821917


  1. for(int i=1; i<=m; i++){
    for(int j=1; j<=n; j++){
    dp = dp [j-1] + 1;
    if(s1.charAt(i-1) == s3.charAt(i+j-1))
    dp = dp[i-1] + 1;
    if(s2.charAt(j-1) == s3.charAt(i+j-1))
    dp = Math.max(dp [j - 1] + 1, dp );
    }
    }
    这里的代码似乎有点问题? dp(i)(j) = dp(i)(j-1) + 1;这个例子System.out.println(ils.isInterleave("aa","dbbca", "aadbbcb"));返回的应该是false

  2. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  3. 这道题目的核心一句话是:取还是不取。
    如果当前取,则index+1作为参数。如果当前不取,则任用index作为参数。