list 的翻转reverse源码:

// 将链表倒置
// 其算法核心是历遍链表, 每次取出一个结点, 并插入到链表起始点
// 历遍完成后链表满足倒置
template <class T, class Alloc>
void list<T, Alloc>::reverse()
{
if (node->next == node || link_type(node->next)->next == node) return;
iterator first = begin();
++first;
while (first != end()) {
iterator old = first;
++first;
transfer(begin(), old, first);
}
}

直接用STL中的list华丽的超时代码:

#include<stdio.h>
#include<iostream>
#include<dequ>
#include<algorithm>
using namespace std;
list<int>li;
list<int>::iterator it,l,r;
int main()
{
int _case,n;
int i,x,lj,rj,step;
scanf("%d",&_case);
while(_case--)
{
li.clear();
scanf("%d",&n);
for(i=; i<n; i++)
{
scanf("%d",&x);
li.push_back(x);
}
scanf("%d%d",&lj,&rj);
l=li.begin();
r=li.begin();
for(i=;i<lj;i++)l++;
for(i=;i<rj;i++)r++;
scanf("%d",&step);
for(i=; i<step; i++)
{
char s[];
char c;
getchar();
scanf("%s",s);
if(s[]=='I')
{
getchar();
scanf("%c%d",&c,&x);
//puts(c);
//printf("%c",c);
if(c=='L')// add before,f b
{
l=li.insert(l,x);
}
else
{
r++;
r=li.insert(r,x);
}
}
else if(s[]=='D')
{
getchar();
scanf("%c",&c);
if(c=='R')
{
r=li.erase(r);
if(r==li.end()&&r!=li.begin())r--;
}
else
{
l=li.erase(l);
//if(l==li.end()&&l!=li.begin())l--;
}
}
else if(s[]=='R')
{
if(r!=li.end())r++;
reverse(l,r);
r--;
}
else
{
getchar();
scanf("%c",&c);
if(s[]=='L')
{ if(c=='L')
{
if(l!=li.begin())l--;
}
else
{
if(r!=li.begin())r--;
//printf("%%\n");
}
}
else
{
if(c=='L')
{
if(l!=li.end())l++;
}
else
{
if(r!=li.end())r++;
}
}
}
//cout<<*l<<' '<<*r<<endl; }
it=li.begin();
printf("%d",*it);
it++;
for(; it!=li.end(); it++)
printf(" %d",*it);
printf("\n");
} return ;
}
 

数组模拟双向链表:

自己写的戳:

#include<stdio.h>
#define Max 1001000
struct node
{
int fr;//front
int be;//behind
int val;//value
bool flag;
} num[Max];;
//int
int n,m,l,r,x;
void Moveleft(char c)
{
if(c=='L'&&num[l].fr!=)
l=num[l].fr;
else
r=num[r].fr;
} void Moveright(char c)
{
if(c=='R'&&num[r].be!=n+)
r=num[r].be;
else
l=num[l].be;
}
void InsertL()
{
num[++n].val=x;
num[n].flag=;
num[n].fr=num[l].fr;
num[n].be=l;
num[num[l].fr].be=n;
num[l].fr=n; l=n;//turn left;
}
void InsertR()
{
printf("##");
num[++n].val=x;
num[n].flag=;
num[n].fr=r;
num[n].be=num[r].be; num[num[r].be].fr=n;
num[r].be=n;
r=n;
}
void DeleteL()
{
num[num[l].fr].be=num[l].be;
num[num[l].be].fr=num[l].fr;
l=num[l].be;
}
void DeleteR()
{
num[num[r].fr].be=num[l].be;
num[num[r].be].fr=num[l].fr;
r=num[r].fr;
}
void Reverse()
{ num[r].flag=;
//
num[num[l].fr].be=r;
num[num[r].be].fr=l;//num[l].fr;
int x=num[l].fr;
num[l].fr=num[r].be;
num[r].be=x; num[num[l].fr].flag=l; //num[num[r].fr].flag=1;
//num[r].be=num[r],fr;
}
void Myprintf()
{
printf("##%d%d\n",l,r);
int ret=;
int x=;
x=num[x].be;
if(x!=m)printf("%d",num[x].val);
ret^=num[x].flag;
if(!ret)//equal 0
x=num[x].be;
else
x=num[x].fr; while(x!=m)
{ printf(" %d",num[x].val); if(!ret)//equal 0
x=num[x].be;
else
x=num[x].fr;
ret^=num[x].flag;
//printf("\n$%d$\n",ret);
}
printf("\n");
}
int main()
{
int _case,step;
int i;
scanf("%d",&_case);
while(_case--)
{
scanf("%d",&n);
num[].be=;
for(i=; i<=n; i++)
{
scanf("%d",&num[i].val);
num[i].fr=i-;
num[i].be=i+;
num[i].flag=;
}
m=++n;
scanf("%d %d",&l,&r);
scanf("%d",&step);
Myprintf();
while(step--)
{
char op[],loc[];
scanf("%s",op);
if(op[]=='R')//reverse
Reverse();
else if(op[]=='I')//insert
{
scanf("%s %d",loc,&x);
if(loc[]=='R')
InsertR();
else
InsertL();
}
else if(op[]=='D')//delete
{
scanf("%s",loc);
if(loc[]=='R')
DeleteR();
else
DeleteL();
}
else//Move
{
scanf("%s",loc);
if(op[]=='R')
Moveright(loc[]);
else
Moveleft(loc[]);
}
Myprintf();
}
Myprintf();
}
return ;
}

参考代码(高明之处 运用了四个左右指针,判断相对位置):

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; struct node
{
int n1,n2,key;
}; node a[];
int L,R,e,n,C,Q,fL,fR; void init()
{
scanf("%d",&n);
int i;
for(i=; i<=n; i++)
{
scanf("%d",&a[i].key);
a[i].n1=i-;
a[i].n2=i+;
}
a[].n2=;
a[n+].n1=n;
e=n+;
scanf("%d%d",&L,&R);
fL=a[L].n1;
fR=a[R].n2;
} void moveLeftL()
{
if(a[fL].n1==L) L=fL,fL=a[fL].n2;
else L=fL,fL=a[fL].n1;
} void moveLeftR()
{
if(a[R].n1==fR) fR=R,R=a[R].n2;
else fR=R,R=a[R].n1;
} void moveRightL()
{
if(a[L].n1==fL) fL=L,L=a[L].n2;
else fL=L,L=a[L].n1;
} void moveRightR()
{
if(a[fR].n1==R) R=fR,fR=a[fR].n2;
else R=fR,fR=a[fR].n1;
} void insertL(int x)
{
a[e].key=x;
a[e].n1=fL;
a[e].n2=L;
if(a[fL].n1==L) a[fL].n1=e;
else a[fL].n2=e;
if(a[L].n1==fL) a[L].n1=e;
else a[L].n2=e;
L=e++;
} void insertR(int x)
{
a[e].key=x;
a[e].n1=R;
a[e].n2=fR;
if(a[R].n1==fR) a[R].n1=e;
else a[R].n2=e;
if(a[fR].n1==R) a[fR].n1=e;
else a[fR].n2=e;
R=e++;
} void deleteL()
{
int next;
if(a[L].n1==fL) next=a[L].n2;
else next=a[L].n1;
if(a[fL].n1==L) a[fL].n1=next;
else a[fL].n2=next;
if(a[next].n1==L) a[next].n1=fL;
else a[next].n2=fL;
L=next;
} void deleteR()
{
int next;
if(a[R].n1==fR) next=a[R].n2;
else next=a[R].n1;
if(a[fR].n1==R) a[fR].n1=next;
else a[fR].n2=next;
if(a[next].n1==R) a[next].n1=fR;
else a[next].n2=fR;
R=next;
} void reverse()
{
if(a[fL].n1==L) a[fL].n1=R;
else a[fL].n2=R;
if(a[L].n1==fL) a[L].n1=fR;
else a[L].n2=fR;
if(a[R].n1==fR) a[R].n1=fL;
else a[R].n2=fL;
if(a[fR].n1==R) a[fR].n1=L;
else a[fR].n2=L; int k;
k=L;
L=R;
R=k;
} void print()
{
bool tag=false;
fL=;
L=a[fL].n2;
while(L!=n+)
{
if(tag) putchar(' ');
else tag=true;
printf("%d",a[L].key);
if(a[L].n1==fL) fL=L,L=a[L].n2;
else fL=L,L=a[L].n1;
}
puts("");
} int main()
{
for(scanf("%d",&C); C--;)
{
init();
scanf("%d",&Q);
char op[],s[];
int X;
while(Q--)
{
scanf("%s",op);
if(!strcmp(op,"MoveLeft"))
{
scanf("%s",s);
if(s[]=='L') moveLeftL();
else moveLeftR();
}
else if(!strcmp(op,"MoveRight"))
{
scanf("%s",s);
if(s[]=='L') moveRightL();
else moveRightR();
}
else if(!strcmp(op,"Insert"))
{
scanf("%s%d",s,&X);
if(s[]=='L') insertL(X);
else insertR(X);
}
else if(!strcmp(op,"Delete"))
{
scanf("%s",s);
if(s[]=='L') deleteL();
else deleteR();
}
else reverse();
}
print();
}
return ;
}

hdu 4286 (list的reverse时间复杂度为n)的更多相关文章

  1. HDU 4286 Data Handler 双向链表/Splay

    Data Handler Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  2. HDU 4286 Data Handler --双端队列

    题意:有一串数字,两个指针,然后一些添加,删除,反转,以及移动操作,最后输出序列. 解法:可以splay做,但是其实双端队列更简便. 维护三个双端队列LE,MI,RI分别表示[L,R]序列左边,[L, ...

  3. hdu 4286

    splay 练手用: 杭电的oj要手动开栈: #include<cstdio> #pragma comment(linker, "/STACK:102400000,1024000 ...

  4. [GodLove]Wine93 Tarining Round #8

    比赛链接: http://vjudge.net/contest/view.action?cid=47644#overview 比赛来源: 2012 ACM/ICPC Asia Regional Tia ...

  5. [NOIP2018]:旅行(数据加强版)(基环树+搜索+乱搞)

    题目描述 小$Y$是一个爱好旅行的$OIer$.她来到$X$国,打算将各个城市都玩一遍.小$Y$了解到,$X$国的$n$个城市之间有$m$条双向道路.每条双向道路连接两个城市.不存在两条连接同一对城市 ...

  6. HDU 1062 Text Reverse(水题,字符串处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 解题报告:注意一行的末尾可能是空格,还有记得getchar()吃回车符. #include< ...

  7. hdu 1062 Text Reverse 字符串

    Text Reverse                                                                                  Time L ...

  8. HDOJ/HDU 1062 Text Reverse(字符串翻转~)

    Problem Description Ignatius likes to write words in reverse way. Given a single line of text which ...

  9. HDU——1062Text Reverse(水题string::find系列+reverse)

    Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

随机推荐

  1. ACM学习历程—TopCoder SRM691 Div2

    这是我的第一次打TC,感觉打的一般般吧.不过TC的题目确实挺有意思的. 由于是用客户端打的,所以就不发题目地址了. 300分的题: 这题大意是有一段序列只包含+和数字0~9. 一段序列的操作是,从头扫 ...

  2. RbbitMQ基础知识

    MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递指的是程序之间 ...

  3. Could not find class 'org.ksoap2.serialization.SoapObject

    Could not find class 'org.ksoap2.serialization.SoapObject工程编译没问题,一在模拟器运行就报错! 这是由于ADT版本过高引发的问题,解决办法: ...

  4. Spring:基于注解的Spring MVC

    什么是Spring MVC Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...

  5. 让maven生成可运行jar包

    平时项目大多用到的是war包,今天实现了一个简单功能,无需部署到web服务器上,只需本地跑java代码即可,因此只要生成一个jar包.那么怎么让maven项目打成一个可以使用java命令跑的jar包呢 ...

  6. 容器中跨主机的网络方案-Weave

    容器中的网络是建立docker集群的重要内容. 本文将介绍如何用Weave实现容器的多节点互通. Weave是一个开源的项目,其网站为: https://www.weave.works/ 其工作原理相 ...

  7. GO数组和切片

    数组Array 定义数组的格式:var <varName>[n]<type>,n>0 数组长度也是类型的一部分,因此具有不同长度的数组为不同类型, 不同类型的不能相互赋值 ...

  8. java随机数组

    import java.util.Random; public class Ccore { public static void main(String[] args) { for(int i=1;i ...

  9. NOIP2008普及组第3题 传球游戏

    NOIP2008普及组第3题 传球游戏 时间限制: 1 Sec  内存限制: 128 MB提交: 29  解决: 16[提交][状态][讨论版][命题人:外部导入] 题目描述 上体育课的时候,小蛮的老 ...

  10. Angular2快速入门-2.创建一个新闻列表

    背景: 我们想通过一个例子,展示下Angular2 怎么绑定页面,怎么创建Component, 例子:我们创建一个新闻列表,当点击新闻列表中某一条新闻的时候,展示出该条新闻的详细信息, 在详细信息中可 ...