## zoj – 3309- Search New Posts

2019年02月10日 算法 ⁄ 共 1579字 ⁄ 字号 评论关闭

//code

```#include<iostream>
#include<cstdio>
#include<string>
#include<list>
#include<map>
using namespace std;

struct List{

List *prev;
List *next;
string name;
friend bool operator < (List a, List b)
{
return a.name<b.name;
}
};

int main()
{
int n;
while(cin>>n)
{
map<string, List*>_m;
map<string, List*>::iterator it;
_l = new List;
_l->name = "Last";
_l->next = NULL;
for(int i=0; i<n; i++)
{
string tmp;
cin>>tmp;
if(tmp == "new")
{

cin>>tmp;
List *_l_tmp;
_l_tmp = new List;
_l_tmp->name = tmp;

_l_tmp->next->prev = _l_tmp;

_m.insert(map<string, List*>::value_type(_l_tmp->name, _l_tmp));
}
{
cin>>tmp;
it = _m.find(tmp);

if(it == _m.end()) continue;
List *Prev, *Next;

Prev = it->second->prev;
Next = it->second->next;

if(Prev != NULL && Next != NULL)
{
Prev->next = Next;
Next->prev = Prev;
}

it->second->next->prev = it->second;
}
else if(tmp == "tag")
{
cin>>tmp;
it = _m.find(tmp);
if(it == _m.end()) continue;
if(it->second->next == NULL && it->second->prev == NULL) continue;
List *Prev, *Next;

Prev = it->second->prev;
Next = it->second->next;

Prev->next = Next;
Next->prev = Prev;

it->second->next = it->second->prev = NULL;

}
else if(tmp == "search")
{
List *q;
int Sum=0;
while(q->next != NULL)
{
cout<<q->name<<endl;
Sum++;
if(Sum >= 100) break;
q = q->next;
}
cout<<"###"<<endl;
}
}
printf("\n");
}
return 0;
}```
【上篇】
【下篇】