codevs 4650 破损的键盘(链表)

codevs 4650 破损的键盘

题目描述 Description
有一天,你需要打一份文件,但是你的键盘坏了,上面的”home”键和”end”键会时不时地按下,而你却毫不知情,甚至你都懒得打开显示器,当你打开显示器之后,出现在你的面前的是一段悲剧的文本。

输入描述 Input Description
输入只有一行,即这份文件,这份文件只包含小写字母和’[‘以及’]’,用’[‘代替”home”键,用’]’代替”end”键。

输出描述 Output Description
你的任务是在打开显示器之前,计算出这份悲剧的文档。

样例输入 Sample Input
kdg[gek]h[itj

de[co]vs

样例输出 Sample Output
itjgekkdgh

codevs

数据范围及提示 Data Size & Hint
包含多组测试数据,直到文件结束。

字符串长度小于10000个字符。

不包含空格。

思路:因为字符长度为10000且多组数据,模拟肯定是不行的,这个地方需要用到链表。

题解I:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int next[100005];
char s[100005];
int main()
{
    while(scanf("%s",s+1)!=EOF)
    {
        int n=strlen(s+1);
        int cur=0,last=0;
        next[0]=0;
        for(int i=1;i<=n;i++)
        {
            if(s[i]=='[')
            {
                cur=0;
            }
            else if(s[i]==']')
            {
                cur=last;
            }
            else
            {
                next[i]=next[cur];
                next[cur]=i;
                if(cur==last)
                {
                    last=i;
                }
                cur=i;
            }
        }
        for(int i=next[0];i!=0;i=next[i])
        {
            printf("%c",s[i]);
        }
        printf("\n");
    }
    return 0;
}

题解II:

额,好像用vector模拟会比较简单。。

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
char a[100000+10];
vector<char>v;
vector<char>::iterator it;
int main()
{
    while(scanf("%s",a+1)!=EOF)
    {
        bool flag=0;
        int n=strlen(a+1);
        int tot=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]!='['&&a[i]!=']')
            {
                if(flag==0)
                {
                    v.push_back(a[i]);
                }
                else
                {
                    v.insert(v.begin()+tot,a[i]);
                    tot++;
                }
            }
            else if(a[i]=='[')
            {
                flag=1;
                tot=0;
            }
            else if(a[i]==']')
            {
                flag=0;
            }
        }
        for(it=v.begin();it!=v.end();it++)
        {
            printf("%c",*it);
        }
        printf("\n");
        v.clear();
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值