Keylogger Gym - 101078I codeforces

这里写图片描述

blablabla: 好久不碰链表。。。但是感觉还可以
1.链表

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>

using namespace std;
char a[1000010];
struct node
{
    char data;
    node *next;
    node *pre;
};
int main()
{
    int n;
    scanf("%d",&n);
    while(n --)
    {
        scanf("%s",a);
        int len = strlen(a);
        node *head,*tail,*p;
        head = new node;
        tail = head;
        tail->next = NULL;
        tail->pre = NULL;
        for(int i=0; i<len; i++)//一直就是因为srelen(a) 超时,每次都算
        {
            if(a[i]=='<')
            {
                if(tail->pre)
                {
                    tail = tail->pre;
                }
            }
            else if(a[i]=='>')
            {
                if(tail->next)
                {
                    tail = tail->next;
                }
            }
            else if(a[i]=='-')
            {
                if(tail!=head)
                {
                   if(tail->next)
                    {
                        tail->pre->next = tail->next;
                        tail->next->pre = tail->pre;
                        tail = tail->pre;
                    }
                    else
                    {
                        tail->pre->next = NULL;
                        tail = tail->pre;
                    }
                }
            }
            else
            {
                p = new node;
                p->data = a[i];
                p->pre = tail;
                p->next = tail->next;
                tail->next = p;
                if(tail->next->next)
                {
                    tail->next->next->pre = p;
                }
                tail  = p;
            }
        }
        tail = head->next;
        while(head->next)
        {
            printf("%c",head->next->data);
            head = head ->next;
        }
        printf("\n");
    }
    return 0;
}

2.STL 顺序表

#include <bits/stdc++.h>
using namespace std;
int t;
char s[2000000];
list <char> ans;
void output()
{
    for(list<char>::iterator it = ans.begin(); it!=ans.end();it++)
        printf("%c",*it);
    puts("");
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s);
        ans.clear();
        list<char>::iterator it, iter;
        for(int i=0,len = strlen(s); i<len;i++)
        {
            if(s[i]=='-')
            {
                if(ans.empty()||it==ans.begin()) continue;
                it--;
                it = ans.erase(it);
            }
            else if(s[i]=='<')
            {
                if(ans.empty()||it==ans.begin()) continue;
                it--;
            }
            else if(s[i]=='>')
            {
                if(ans.empty()||it==ans.end()) continue;
                it++;
            }
            else
            {
                if(ans.empty())
                {
                    ans.push_back(s[i]);
                    it = ans.end();
                }
                else
                {
                    it = ans.insert(it,s[i]);
                    it++;
                }
            }
        }
        output();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值