计蒜客练习题:网页跳转(栈+java+计蒜客坑爹)

import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        Stack<String> url=new Stack<String>();
        Stack<String> url_out=new Stack<String>();
        int n=cin.nextInt();
        for(int i=0;i<n;i++){
            String s=cin.next();
            if(s.equals("VISIT")){
                String url_s=cin.next();
                System.out.println(url_s);
                url.push(url_s);
                url_out.clear();
            }
            if(s.equals("BACK")){
                if(!url.isEmpty())
                    url_out.push(url.pop());
                if(url.isEmpty()){
                    System.out.println("Ignore");
                    if(!url_out.isEmpty())
                        url.push(url_out.pop());
                }else{
                    System.out.println(url.peek());
                }    
                
            }
            if(s.equals("FORWARD")){
                if(url_out.isEmpty()){
                    System.out.println("Ignore");
                    
                }else{
                    System.out.println(url_out.peek());
                    url.push(url_out.pop());
                }
            }
        }
        cin.close();
    }
}
View Code

先附JAVA AC代码。

不得不说,这道题是真的很坑啊。

首先这道题的思路很简单,用两个栈,一个存储前景,一个存储后退。当visit时记得把前进栈清空即可。

但是就是超时啊。之后“贴心“的计蒜客就会给你提示:

哦,原来是忘记关cin同步了。当你加上这一句后,你以为你这次一定会过了。然而...

这怎么会超时?之后我前前后后改了3个小时左右,一直都是超时超时超时,我都开始怀疑是不是我的算法出了问题。

之后就去网上贴代码,但是!网上的代码一个能AC的都没有!!!这道题给改了样例了!我那个绝望啊。

计蒜客这题没写掉,后面的题目都写不了!!我那个难受啊。计蒜客生涯止步于此了吗?

之后我听同学说要用Java写。。。

JAVA牛逼!

这道题真的是给了我很大的启发。当你觉得算法没问题,时间没问题,各种东西都没问题的时候,

一定要尝试其他的语言,毕竟除C语言以外的其他语言时限都是C语言的两倍。

计蒜客也太坑了。

之后附上我觉得一点问题都没有的代码

#include <iostream>
#include <stack>
#include <string>
using namespace std;
//vector<string> s1(100000), s2(100000);    
stack<string> s1;
stack<string> s2;
int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(0);   
    int n;
    cin >> n;
    while (n--) {
        string str1, str2;
        cin >> str1;
        if (str1 == "VISIT") {
            cin >> str2;
            while (!s2.empty()) {
                s2.pop();
            }
            s1.push(str2);
            cout << s1.top() << endl;
        }
        else if (str1 == "BACK") {
            if (s1.empty()) {
                cout << "Ignore" << endl;
            }
            else {
                s2.push(s1.top());
                s1.pop();
                if (!s1.empty()) {
                    cout << s1.top() << endl;
                }
                else {
                    cout << "Ignore" << endl;
                    s1.push(s2.top());
                    s2.pop();
                }
            }
        }
        else {
            if (s2.empty()) {
                cout << "Ignore" << endl;
            }
            else {
                if (!s2.empty()) {
                    cout << s2.top() << endl;
                    s1.push(s2.top());
                    s2.pop();
                }
                else {
                    cout << "Ignore" << endl;
                }
            }

        }
    }
}
View Code

如果有人能找出错误,请一定要告诉我!

猜你喜欢

转载自www.cnblogs.com/Vikyanite/p/11749003.html