算法竞赛入门经典第六章例题6-4 Broken Keyboard (a.k.a. Beiju Text) UVA - 11988

本文介绍了一种用于处理字符串的导航算法,该算法通过使用特殊的字符来实现字符串中的位置移动,例如移动到开头('[')或结尾(']')。此外,还提供了一个C++实现示例,展示了如何使用额外的数组来记录当前位置并进行相应的操作。

https://vjudge.net/problem/UVA-11988

#include<iostream>
#include<cstring>
using namespace std;
#pragma warning(disable:4996)
int cur,last,nex[100005];
char s[100005];
int main() {
#ifdef _DEBUG
    //freopen("in", "r", stdin);
    //freopen("out", "wb", stdout); 
#endif // _DEBUG
    while (scanf("%s",s+1)==1) {
        int n = strlen(s + 1);
        cur = last = nex[0] = 0;
        for(int i=1;i<=n;++i)
            switch (s[i])
            {
            case '['://Home
                cur = 0;
                break;
            case ']'://End
                cur = last;
                break;
            default:
                nex[i] = nex[cur];
                nex[cur] = i;
                if (cur == last) last = i;
                cur = i;
                break;
            }
        for (int i = nex[0]; i != 0; i = nex[i])
            cout << s[i];
        cout << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值