B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表

针对一个特殊键盘问题,即有时会自动按下“Home”或“End”键的情况,本篇介绍了一种解决方法来还原正确的输入文本。通过两种不同的实现方式——使用链表和数组模拟链表,最终实现了高效且准确的文本修正。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Input

There are several test cases. Each test case is a single line containing at least one and at most 100,000
letters, underscores and two special characters ‘[’ and ‘]’. ‘[’ means the “Home” key is pressed
internally, and ‘]’ means the “End” key is pressed internally. The input is terminated by end-of-file
(EOF).

Output

For each case, print the Beiju text on the screen.

Sample Input

This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University

Sample Output

BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University

破碎的键盘,之前的c语言作业,当时就没过,然后一开始想用vector,但是这个插入不行,会T,然后用链表写了一下。。吐血,不知道为什么也会T;

会T的链表代码

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h> 
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
//#define pb push_back
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
//#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e9+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
char a[100005];
struct node
{
    char c;
    node *next;
};
int main()
{
    int x;
    while(~sf("%s",&a))
    {
        node *head,*now,*last,*p;
        head=new node;
        int temp=0;
        last=now=head;
        head->next =NULL;
        rep(i,0,strlen(a))
        {
            if(a[i]=='[')
            {
                now=head;
                temp=1;
            }
            else if(a[i]==']')
            {
                now=last;
                temp=0;
            }
            else
            {
                p=new node;
                p->c =a[i];
                p->next=now->next ;
                now->next =p;
                now=p;
                if(temp==0)
                last=now;
                while(last->next!=NULL) last=last->next;
            }
        }
        last->next =NULL;
        p=head->next;
        while(p!=NULL)
        {
            pf("%c",p->c);
            now=p;
            p=p->next;
            free(now);
        }
        pf("\n");
    }
    return 0;
    
}

后面看到刘汝佳书上有讲这题,然后照着这种方法过了p143,用数组模拟链表,因为是从左到右的,所以可以只用一个NEXT数组储存右边跟着的字符的下表就好了;

AC的

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h> 
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
//#define pb push_back
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
//#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e9+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
char s[100005];
int NEXT[100005],last,cur;
int main()
{
    int n,last,cur;

    while(sf("%s",s+1)!=EOF)
    {
         n=strlen(s+1);
        last=cur=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])
        pf("%c",s[i]);
        pf("\n");
    }
    return 0;
}

转载于:https://www.cnblogs.com/wzl19981116/p/9403866.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值