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. fpga加法进位链实现过程中的一个特点

    altera fpga 用quartus综合后会出现加法进位链一正一反的情况,所谓一正一反指的是假设某一级输入为a,b,进位值为c,则该级进位链逻辑应该为cout=ab+ac+bc,但实际为 cout ...

  2. Linux性能评测工具之一:gprof篇介绍

    转:http://blog.csdn.net/stanjiang2010/article/details/5655143 这些天自己试着对项目作一些压力测试和性能优化,也对用过的测试工具作一些总结,并 ...

  3. .Net Remoting编程 ---- 系列文章

    .Net Remoting(应用程序域) - Part.1 摘要: 本文是.Net Remoting系列的第一篇文章,讲述了Remoting的“前驱知识点”--应用程序域.传值封送(Marshal b ...

  4. ArcGIS_Lisence安装步骤

    1.双击lisence.exe文件 2.下一步 3.关闭 4.下一步 5.下一步 6.下一步 7.安装 8.完成 9.OK                      

  5. Weblogic配置SSl使用Https

    一 .可以开启自带的SSL连接 启动weblogic,进入左侧菜单,点击左侧的安全领域-->点击myrealm-->点击角色和策略-->点击服务器AdminServer 点击保存,w ...

  6. 10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides(转)

    10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides Wednesday, Janua ...

  7. js对字符串进行编码方法总结

    在用javascript对URL字符串进行编码中,虽然escape().encodeURI().encodeURIComponent()三种方法都能对一些影响URL完整性的特殊字符进行过滤.但后两者是 ...

  8. Linux环境变量从用户配置改为系统配置

    部署了一个新的tomcat到一个新的用户下,发下启动失败了 /home/personal/apache-tomcat/bin/catalina.sh: line 434: /usr/lib/jvm/j ...

  9. 如何让公司从SVN改到Git?

    把公司的SVN迁移到GitLab CE(GitLab社区版)原因主要有下面几个: 年青的新人进来,喜欢用git的越来越多 GitLab CE提供了优美的 web 界面,图形化分支结构,更直观的代码审查 ...

  10. python selenium中iframe切换、window切换方法

    一.selenium中iframe切换方法: 方法一:switch_to.frame frame函数中提供了三种定位方法:by index, name, or webelement. driver.s ...