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. 老罗关于binder的链接

    Android进程间通信(IPC)机制Binder简要介绍和学习计划 : http://blog.csdn.net/luoshengyang/article/details/6618363

  2. AVAWEB学习笔记 ---- 系列文章

    [JAVAWEB学习笔记]网上商城实战5:后台的功能模块 [JAVAWEB学习笔记]网上商城实战4:订单模块 [JAVAWEB学习笔记]网上商城实战3:购物模块和订单模块 [JAVAWEB学习笔记]网 ...

  3. Java之父职场路

    Java之父——詹姆斯·高斯林出生于加拿大,是一位计算机编程天才.在卡内基·梅隆大学攻读计算机博士学位时,他编写了多处理器版本的Unix操作系统,是JAVA编程语言的创始人.1991年,在Sun公司工 ...

  4. Windows下通过Composer安装Yii2 [ 2.0 版本 ]

    安装好大于5.4或更高版本的PHP环境并开启openssl扩展.如果是Apache服务器,加载Apache的mod_ssl模块. 下载Composer并安装. 开始->运行[或者WIN+R]-& ...

  5. bzoj 2159 Crash 的文明世界 && hdu 4625 JZPTREE ——第二类斯特林数+树形DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2159 学习材料:https://blog.csdn.net/litble/article/d ...

  6. Android 中jar包封装及调用-转

    在android开发过程中,我们经常会有这种需求,自己开发一个类库jar包,提供给别人调用. 即把项目A封装成jar包,供项目B调用,而在项目B中调用项目A的activity的时候问题就出现了:找不到 ...

  7. 几大PHP套件

    UPUPW:http://www.upupw.net/ PHPStudy:http://www.phpstudy.net/ PHPNow:http://servkit.org/

  8. 常用FTP命令 1. 连接ftp服务器

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...

  9. Annotation之二:@Inherited注解继承情况

    @Inherited annotation类型是被标注过的class的子类所继承.类并不从它所实现的接口继承annotation,方法并不从它所重载的方法继承annotation. 子类中能否继承注解 ...

  10. Java-Maven-Runoob:Maven 构建配置文件

    ylbtech-Java-Maven-Runoob:Maven 构建配置文件 1.返回顶部 1. Maven 构建配置文件 构建配置文件是一系列的配置项的值,可以用来设置或者覆盖 Maven 构建默认 ...