手写链表 #includecstdio#includecstringusing namespace std;char s;int tem,head,nex,last;struct word{ char c; int next; int last;}seq[1000100];int main(){ int i,f=1; //freopen("a.txt","r",stdin); //freopen("b.txt","w",stdout); while(scanf
手写链表
#include<cstdio>
#include<cstring>
using namespace std;
char s;
int tem,head,nex,last;
struct word{
char c;
int next;
int last;
}seq[1000100];
int main(){
int i,f=1;
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
while(scanf("%c",&s)!=EOF){
if(s=='\n'){
if(tem==0)continue;
tem=head;
while(tem!=-1){
printf("%c",seq[tem].c);
tem=seq[tem].next;
}
printf("\n");
}
if(s=='\n'||f){
tem=0;last=-1;head=0;nex=-1;
f=0;
memset(seq,0,sizeof(seq));
}
if(s=='\n')continue;
if(s!='L' && s!='R'){
seq[tem].c=s;
seq[tem].last=last;
seq[tem].next=nex;
if(last!=-1)
seq[last].next=tem;
else if(last==-1)
head=tem;
if(nex!=-1)
seq[nex].last=tem;
tem++;
seq[tem].last=tem-1;
last=tem-1;
}
else if(s=='L'){
if(last==-1)continue;
nex=last;
last=seq[last].last;
}
else if(s=='R'){
if(nex==-1)continue;
last=nex;
nex=seq[nex].next;
}
}
}
用stl的list
#include <cstdio>
#include <cstring>
#include <list>
using namespace std;
char org[1000001];
int main(void) {
list<char> word;
scanf ("%s", org);
int i;
int len = strlen(org);
list<char>::iterator it = word.begin();
for (i = 0; i < len; ++i) {
if (org[i] == 'L') {
if (it != word.begin()) {
--it;
}
} else if (org[i] == 'R') {
if (it != word.end()) {
++it;
}
} else {
word.insert(it, org[i]);
}
}
for (it = word.begin(); it != word.end(); ++it) {
printf ("%c", *it);
}
printf ("\n");
return 0;
}