标准的网页浏览器都提供一个功能:保留最近浏览过页面的历史记录。通过后退或向前按钮就能在历史记录之间跳转。
现在,请你模拟这个功能,接收如下三条指令:
1. BACK:回退功能,即回退到上一个访问的页面;
2. FORWARD:使用BACK返回上一页之后,可以使用FORWARD回到下一页;
3. VISIT url:访问指定url的页面,并且所有FORWARD的页面都被清空。
输入包含多组数据,每组数据第一行包含一个正整数n(1≤n≤100)。
紧接着有n行,每一行包含一条指令。其中url是不包含空格、长度不超过100的非空字符串。
对应每组数据,为每条指令输出当前页面的URL。
如果当前指令无效(例如没有上一页时执行BACK指令、或没有下一页时执行FORWARD指令),则输出一行“ignore”。
每组数据之后输出一个空行作为分隔。
13 VISIT http://www.acm.org/ VISIT http://acm.ashland.edu/ VISIT http://acm.baylor.edu/acmicpc/ BACK BACK BACK FORWARD VISIT http://www.ibm.com/ BACK BACK FORWARD FORWARD FORWARD
http://www.acm.org/ http://acm.ashland.edu/ http://acm.baylor.edu/acmicpc/ http://acm.ashland.edu/ http://www.acm.org/ ignore http://acm.ashland.edu/ http://www.ibm.com/ http://acm.ashland.edu/ http://www.acm.org/ http://acm.ashland.edu/ http://www.ibm.com/ ignore
#include<iostream> #include<vector> #include<string> #include<algorithm> #include<functional> #include <map> #include <set> #include <unordered_set> #include <unordered_map> #include <exception> #include <iomanip> #include <memory> #include <sstream> using namespace std; int main(int argc, char** argv) { //freopen("in.txt", "r", stdin); int n; while (cin >> n) { vector<string> urls; int cur = -1; string op, url; for (int i = 0; i < n; ++i) { cin >> op; if (op == "VISIT") { urls.erase(urls.begin() + (cur + 1),urls.end()); cin >> url; urls.emplace_back(url); ++cur; cout << url << endl; } else if (op == "BACK") { if (cur <= 0) cout << "ignore" << endl; else { --cur; cout << urls[cur] << endl; } } else { if (cur >= static_cast<int>(urls.size()) - 1) cout << "ignore" << endl; else { ++cur; cout << urls[cur] << endl; } } } cout << endl; } return 0; }
#include <iostream> #include <stack> #include <string> using namespace std; int main() { int n; string op; string url; while(cin >> n){ stack<string> BACK; stack<string> FORWARD; while(n--){ cin >> op; if(op == "VISIT"){ cin >> url; BACK.push(url); while(!FORWARD.empty()) FORWARD.pop(); cout << url << endl; } else if(op == "BACK"){ if(BACK.empty() || BACK.size() == 1){ cout<< "ignore" << endl; continue; } url = BACK.top(); BACK.pop(); FORWARD.push(url); url = BACK.top(); cout << url << endl; } else if(op == "FORWARD"){ if(FORWARD.empty()){ cout << "ignore" << endl; continue; } url = FORWARD.top(); FORWARD.pop(); BACK.push(url); cout << url << endl; } } cout << endl; } return 0; }
// write your code here cpp #include<cstdio> (802)#include<stack> using namespace std; int main(){ char str[110],url[110][110]; int n; while(scanf("%d",&n)!=EOF){ int k=0; stack<char*> s1,s2; for(int i=0;i<n;++i){ scanf("%s",str); if(str[0]=='V'){ scanf("%s",url[k]); printf("%s\n",url[k]); s1.push(url[k++]); while(!s2.empty()){ s2.pop(); } }else if(str[0]=='B'){ if(s1.empty()||s1.size()==1){ printf("ignore\n"); }else{ s2.push(s1.top()); s1.pop(); printf("%s\n",s1.top()); } }else if(str[0]=='F'){ if(!s2.empty()){ printf("%s\n",s2.top()); s1.push(s2.top()); s2.pop(); }else{ printf("ignore\n"); } } } printf("\n"); } return 0; }
#include <iostream> #include <string> #include<vector> using namespace std; int main() { int length; string ignore = "ignore"; while (cin >> length) { getchar(); vector<string> First, Second, res; vector<string>::iterator it = First.begin(); string s; for (int i = 0; i < length; i++) { getline(cin, s); if (s == "BACK") { if (First.size() == 1 || First.size() == 0) cout << ignore << endl; else { string t = First[First.size() - 2]; cout << t << endl; res.push_back(t); t = First[First.size() - 1]; Second.push_back(t); First.pop_back(); } } else if (s == "FORWARD") { if (Second.size() == 0) cout << ignore << endl; else { string t = Second[Second.size() - 1]; Second.pop_back(); First.push_back(t); res.push_back(t); cout << t << endl; } } else { Second.clear(); string t; for (int i = 6; i < s.length(); i++) t += s[i]; cout << t << endl; First.push_back(t); } } cout<<endl; } return 0; }
// write your code here import java.util.ArrayList; import java.util.Scanner; public class Main{ public static void main(String[] args){ ArrayList<String> alist = new ArrayList<String>(); Scanner in = new Scanner(System.in); String action; int curloc = -1; while(in.hasNext()){ action = in.next(); if(action.compareTo("BACK") == 0){ if(curloc <= 0 ) System.out.println("ignore"); else{ curloc--; System.out.println(alist.get(curloc)); } } else if(action.compareTo("VISIT") == 0){ for(int i = curloc + 1; i < alist.size(); i++) alist.remove(i); action = in.next(); alist.add(action); curloc++; System.out.println(action); } else if(action.compareTo("FORWARD") == 0){ if(curloc == -1 || curloc >= (alist.size() - 1)) System.out.println("ignore"); else{ curloc++; System.out.println(alist.get(curloc)); } } else{ curloc = -1; alist.clear(); } } in.close(); } }为什么我在eclipse上的运行结果和这上面的不一样?
#include<stack> #include<vector> #include<iostream> using namespace std; bool isurl(string &s){ return s!="BACK"&&s!="FORWARD"; } int main(){ int n; while(cin>>n){ vector<string> array; stack<string> backStack,forwardStack; string cur_url=""; //输入初始化 for(int i=0;i<n;i++){ string s; cin>>s; if(s=="VISIT")cin>>s; //这句很关键,同时也消除了VISIT array.push_back(s); } //开始操作 int len = array.size(); for(int i=0;i<len;i++){ if(array[i]=="BACK"){ if(backStack.empty()){ cout<<"ignore"<<endl; }else{ forwardStack.push(cur_url); cur_url=backStack.top(); cout<<cur_url<<endl; backStack.pop(); } }else if(array[i]=="FORWARD"){ if(forwardStack.empty()){ cout<<"ignore"<<endl; }else{ backStack.push(cur_url); cur_url=forwardStack.top(); cout<<cur_url<<endl; forwardStack.pop(); } }else{ if(cur_url==""){ cur_url=array[i]; }else{ backStack.push(cur_url); cur_url=array[i]; } //3. VISIT url:访问指定url的页面,并且所有FORWARD的页面都被清空。 while(!forwardStack.empty()){ forwardStack.pop(); } cout<<array[i]<<endl; } } //每组数据之后输出一个空行作为分隔 cout<<endl; } return 0; }
#include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; int main() { int n; while (cin >> n && n) { vector<string> urls; string cmd, url; int valid = 0, index = -1; for (int i = 0; i < n; ++i) { cin >> cmd; if (cmd == "VISIT") { urls.erase(urls.begin() + (index + 1), urls.end()); valid = index + 1; cin >> url; urls.push_back(url); ++valid; ++index; cout << url << endl; } else if (cmd == "BACK" && index > 0) cout << urls[--index] << endl; else if (cmd == "FORWARD" && index < valid - 1) cout << urls[++index] << endl; else cout << "ignore" << endl; } cout << endl; } return 0; }
package newcoder_test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class WebWork { private static int index = -1;// 当前网页的记录 private static int lately = -1;// 最新输入的网页 public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader bufr = new BufferedReader(new InputStreamReader( System.in)); String line = null; int num = Integer.parseInt(bufr.readLine());// 第一行输入的数字 List<String> input = new ArrayList<String>();// 记录每一行的输入 for (int i = 0; i < num; i++) { line = bufr.readLine(); input.add(line); } List<String> result = new ArrayList<String>();// 记录过的浏览网页 for (int i = 0; i < input.size(); i++) { run(result, input.get(i)); } bufr.close(); } public static void run(List<String> list, String order) { if ("BACK".equalsIgnoreCase(order)) {// 返回 if (index == 0)// 如果当前为第一个网页记录将无法继续后退一步 System.out.println("ignore"); else System.out.println(list.get(--index)); } else if ("FORWARD".equalsIgnoreCase(order)) {// 前进 if (index >= lately)// 如果当前为最新的网页记录将无法继续前进一步 System.out.println("ignore"); else System.out.println(list.get(++index)); } else {// 访问 if (!"VISIT".equalsIgnoreCase(order.substring(0, 5))) { System.out.println("error"); } else { list.add(++index, order.substring(6));//在当前网页的后面插入新的网页 lately = index;//记录下此时最新输入的网页 // index++; System.out.println(list.get(index)); } } } }