sgu 397

手写链表

#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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值