UVA11988——悲剧文本

题目大意:        

        You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem with the keyboard is that sometimes the "home" key or the "end" key gets automatically pressed (internally).

You're not aware of this issue, since you're focusing on the text and did not even turn on the monitor! After you finished typing, you can see a text on the screen (if you turn on the monitor).

        In Chinese, we can call it Beiju. Your task is to find the Beiju text.

分析:

本题可以使用链表或者双向队列解,此处使用链表,方法是每输入一个字符就把他存起来,设输入字符串储存在s[1-n],则可以使用next[i]表示在当前显示屏中s[i]右边的字符编号,即s的下标。

为方便起见,假设字符串s最前面还有一个虚拟的s[0],则next[0]就可以表示显示屏中最左边的字符。在使用一个cur表示光标的位置:即当前光标位于s[cur]的右边。cur=0;说明光标位于“虚拟字符”s[0]的右边,即显示屏的最左端。同时,为了移动光标,我们需要一个变量last表示显示屏的最后一个字符是s[last]。

代码:

#include<cstdio>
#include<cstring>

//#define file
#define PI 3.1415926
#define MAX(A,B) ((A)>(B)?(A):(B))
#define ll long long
#define fordo(A,B,C) for(int (A)=(B);(A)<=(C);(A)++)
using namespace std;

const int maxn = 100000 + 50 ;
int last,cur,next[maxn];//光标的位置在cur后的一个字符
char s[maxn];//大数组开全局

int main()
{
    #ifdef file
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
    #endif // file
    while(scanf("%s",s+1)==1)
    {
        int n=strlen(s+1);
        last=cur=0;
        next[0]=0;

        //for(int i=1;i<=n;i++)
        fordo(i,1,n)
        {
            char ch=s[i];
            if(ch=='[')
                cur=0;
            else if(ch==']')
                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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值