这题咋做啊?

上网

时间:0.2s 空间:32M

题目描述:

现在三种浏览网页的行为,跟我们平时上网情景一样

visit: 访问某个特定的网址,相当于我们在当前页面的地址栏输入一个url

back: 回到上一次访问的网址,相当于下图中的左箭头

forward: 相当于下图中的右箭头。

要注意,一旦visit操作之后,右箭头就会消失,因为刚才输入的网址已经是最新网址,没有更新的了,可以自己试试再来做这个题。

假设一开始正在访问的第一个网址为 http://www.hzxjhs.com/

输入格式:

输入若干行,碰到"QUIT"结束,表示关闭浏览器

其他一共三种输入类型。

VISIT 操作后面会跟一个字符串,表示网址

BACK

FORWARD

输出格式:

对于每个操作,输出当前访问的网址,若操作非法,输出"Ignored"

**样例输入:

VISIT http://www.codeforces.com/ VISIT http://www.topcoder.com/ BACK BACK BACK FORWARD VISIT http://www.codechef.com/ BACK BACK FORWARD FORWARD FORWARD QUIT

样例输出:

http://www.codeforces.com/ http://www.topcoder.com/ http://www.codeforces.com/ http://www.hzxjhs.com/ Ignored http://www.codeforces.com/ http://www.codechef.com/ http://www.codeforces.com/ http://www.hzxjhs.com/ http://www.codeforces.com/ http://www.codechef.com/ Ignored

约定:

所有的网址都没有空格,最长的网址为70

总的操作次数不超过1000

按题意模拟啊

#include<bits/stdc++.h>
using namespace std;
string a[100000];
int main(){
	int i=1,j=1;
	string s;
	a[0]="http://www.hzxjhs.com/"; 
	while(cin>>s){
		if(s=="QUIT"){
			break;
		}
		if(s=="VISIT"){
			i++;
			cin>>a[i];
			j=i;
			cout<<a[i]<<endl;
		}
		if(s=="BACK"){
			if(j==0){
				cout<<"Ignored"<<endl;
                                continue;
			}
			j--;
			cout<<a[j]<<endl;
		}
		if(s=="FORWARD"){
			if(j==i){
				cout<<"Ignored"<<endl;
                                continue;
			}
			j++;
			cout<<a[j]<<endl;
		}
	} 
	return 0;
} 

不知道哪错了

???

1 个赞

没有提示,编译正常

1 个赞

额…

1 个赞

back 时,j=1 输出Ignored

1 个赞

还是不对

1 个赞

他第一个网站是 http://www.hzxjhs.com/ ,不是输入进去的,我存在a[0]里,不能等于1

1 个赞

#include<bits/stdc++.h>
using namespace std;
string b[30001],a[30001];
int s=0,top=1;
void push(string x){ //入栈
top++;
a[top]=x;
}
void pop(){ //出栈
top–;
}
string t(){ //读取栈顶
return a[top];
}
int size(){ //栈内元素个数
return top;
}
void pus(string x){ //入栈
s++;
b[s]=x;
}
void po(){ //出栈
s–;
}
string tt(){ //读取栈顶
return b[s];
}
int siz(){ //栈内元素个数
return s;
}
int main(){
string s1,s2;
int i=0,fd=0,bk=0;
a[1]=“http://www.hzxjhs.com/”;
while(cin>>s1){
string s2;
if(s1==“QUIT”)
break;
if(s1==“VISIT”){
cin>>s2;
push(s2);
s=0;
cout<<s2<<endl;
}
if(s1==“BACK”){
if(top==1){
cout<<“Ignored”<<endl;
}
else{
pus(a[top]);
pop();
cout<<t()<<endl;
}
}
if(s1==“FORWARD”){
if(siz()==0)
cout<<“Ignored”<<endl;
else{
cout<<tt()<<endl;
push(b[s]);
po();
}
}
}
return 0;
}
有用的话给个解决方案

谢谢,已解决

1 个赞

在线求解决方案 :hand_with_index_finger_and_thumb_crossed: :hand_with_index_finger_and_thumb_crossed: