一道ACM题

lvlu911 2009-04-30 07:41:24
/*
题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1028
编译器:DEVCPP
*/
#include<iostream>
#include<string>
using namespace std;
typedef struct node{
string url;
node* next;
}node,*link;
class UrlStack{
public:
UrlStack();
~UrlStack();
void push(link&);
void pop(link&);
void clear();
private:
link head;
};
void browserInit();
UrlStack::UrlStack(){
head=new node;
head->next=NULL;
}
UrlStack::~UrlStack(){
link p=head;
while(p){
link q=p->next;
delete p;
p=q;
}
}
void UrlStack::push(link& p){
if(p){
p->next=head->next;
head->next=p;
p=NULL;
}
}
void UrlStack::pop(link& p){
p=head->next;
if(p){
head->next=p->next;
p->next=NULL;
}
}
void UrlStack::clear(){
link p=head->next;
while(p){
link q=p->next;
delete p;
p=q;
}
}
void browserInit(){
UrlStack back,forward;
link current=new node,p=NULL;
current->url="http://www.acm.org/";
current->next=NULL;
string str;
while(getline(cin,str)&&str!="QUIT"){
if(str=="BACK"){
back.pop(p);
if(p){
forward.push(current);
current=p;
p=NULL;
cout<<current->url<<endl;
}
else cout<<"Ignored\n";
}
else if(str=="FORWARD"){
forward.pop(p);
if(p){
back.push(current);
current=p;
p=NULL;
cout<<current->url<<endl;
}
else cout<<"Ignored\n";
}
else{
forward.clear();
back.push(current);
current=new node;
str.assign(&str[6]);
current->url=str;
current->next=NULL;
cout<<current->url<<endl;
}
}
delete current;
}
int main(){
browserInit();
system("PAUSE");
return 0;
}
/*
测试数据: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
QUIT
要求输出:
http://acm.ashland.edu/
http://acm.baylor.edu/acmicpc/
http://acm.ashland.edu/
http://www.acm.org/
Ignored
http://acm.ashland.edu/
http://www.ibm.com/
http://acm.ashland.edu/
http://www.acm.org/
http://acm.ashland.edu/
http://www.ibm.com/
Ignored
我的结果:
http://acm.ashland.edu/
http://acm.baylor.edu/acmicpc/
http://acm.ashland.edu/
http://www.acm.org/
Ignored
http://acm.ashland.edu/
http://www.ibm.com/
http://acm.ashland.edu/
http://www.acm.org/
http://acm.ashland.edu/
http://www.ibm.com/
http://www.ibm.com/
*/

小弟第一次发帖,请大牛们多多支持!
...全文
285 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihao102 2009-05-01
  • 打赏
  • 举报
回复
楼主,只要把后退,前进的原理搞懂了,这个题目就简单了。
lvlu911 2009-05-01
  • 打赏
  • 举报
回复
第一次发帖啊,本来代码是有缩进的,发上来不知怎么就全左对齐了。结贴了。。。
BuleRiver 2009-05-01
  • 打赏
  • 举报
回复
链表:list<string>
lihao102 2009-05-01
  • 打赏
  • 举报
回复
我已经AC了。一年前的事。
nwao7890 2009-04-30
  • 打赏
  • 举报
回复
我没看明白。ACM的题应该主要考算法吧
adfas 2009-04-30
  • 打赏
  • 举报
回复
关键是你的代码。。。。。
没有注释,没有良好的风格,。。。。
lvlu911 2009-04-30
  • 打赏
  • 举报
回复
我也是查不出来才发上来请教的。
liao05050075 2009-04-30
  • 打赏
  • 举报
回复
是啊。查错的过程是很痛苦的。看不是自己写的代码更痛苦。
做ACM是一个过程,这个过程既有AC的快乐,也是WA的郁闷。
LZ啊,找错也是练习的重要内容,要学会自己找出错误才行。
不然比赛时就麻烦了。
adfas 2009-04-30
  • 打赏
  • 举报
回复
我宁愿做10个 acm,也不愿意看你哪里错了
liliangbao 2009-04-30
  • 打赏
  • 举报
回复
帮顶~
lvlu911 2009-04-30
  • 打赏
  • 举报
回复
我只想知道我的为什么错了。。。
adfas 2009-04-30
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
#include <vector>
typedef struct NodeT
{
NodeT * prev;
NodeT * next;
std::string url;
NodeT(NodeT * p, NodeT * n, std::string u)
{
prev = p;
next = n;
url = u;
}
} Node;
using namespace std;

int main()
{
string s1;
Node *p;
p = new NodeT(0, 0, "http://www.acm.org/");

while (cin >> s1)
{

if (s1 == "VISIT")
{
cin >> s1;

p->next = new NodeT(p, 0, s1);
p = p->next;

cout << p->url << endl;
}
else if (s1 == "BACK")
{
if (p->prev != 0)
{
p = p->prev;

cout << p->url << endl;
}
else
cout << "Ignored" << endl;


}
else if (s1 == "FORWARD")
{
if (p->next != 0)
{
p = p->next;
cout << p->url << endl;
}
else
cout << "Ignored" << endl;
}
else
break;

}

}


没技术含量的问题。
双向链表搞定

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧