文本编辑器

· · 个人记录

基本概念

题面先不解释了,大家肯定都懂了 ~(肯定不是我没时间写了)~

可以发现,用普通的单/双向链表不是很好做(不是不能做),于是我们引入一种链表——无向链表。

无向链表是什么呢?顾名思义,就是没有前驱后继的链表,类似无向图的链,这种链表有一种优势:他的行走方向可以任意规定,不像单/双向链表,规定了输出必须走后继。

无向链表在这个题里面有啥用呢?对于时间复杂度瓶颈区间翻转操作,只需要修改两个指针位置的链表指向,就可以做到不管中间节点,中间节点自动翻转。

譬如:

文本原来的路径是:

A - B - ... - C - D

翻转后变成

A - C - ... - B - D

我们只需要:

就可以做到区间翻转了,而且区间内的节点没有修改。

具体实现

翻转操作上面说的差不多了,下面就是其他操作的实现了。

首先用两个 pair 存左右光标分别在哪两个位置之间。

这个题比较恶心,会出现左光标在右光标的右边这种情况,为了处理这种情况,我们需要用一个 flag 标记左光标是否在右光标左边,如果是 flag 为 1,否则为 0。

对于链表,由于 STL 中没有直接存无向链表的东西,所以我们用 vector<node> 存储,其中 node 为自定义结构体,表示链表的每个节点。

在 node 中,我们用一个 pair 存邻居节点,一个 char 表示这个点的字符,如果有一个邻居没有,那么 pair 的这个位置就存为 0。

接下来是一些辅助函数:

建边

newedge 函数,用于连接两个点 x 和 y,因为默认这两个点之间没有连接,所以我们只需要看 x 和 y 中 pair 哪个位置为 0,就把这个位置设为对方。

删边

deleteedge 函数,用于断开两个点 x 和 y 的连接,因为默认两个点有连接,所以我们只需要看 x 和 y 中 pair 哪个位置为对方,就把这个位置设为 0。

以 y - x 的方向 x 的下一个下标

nxt 函数,用于给定两个节点 x 和 y,默认行走的方向为 y - x,走向下一个节点是哪个。只需要看 x 的 pair 中哪个不是 y,哪个就是下一个节点。

判断重叠

cursor 函数,判断左右光标是否重叠。

操作方法:

向左与向右移动

某个光标向左和向右移动,思路是一样的,只拿 向左移 举例。设当前光标的 pair 为 p。首先如果当前光标在链表的头和第一个元素之间,也就是 p.first == head,那么这个时候就应该返回 0 代表操作失败。将 p.first 变为从 p.second - p.first 这个方向下一个节点,也就是 nxt(p.first, p.second),p.second 变为原来的 p.first。如果说这个题不存在左光标在右光标右边的情况,那这个函数就到此为止了,可并不是。所以这个时候更新 flag,如果当前指针是左光标,即 w == L 那么我们需要判断,如果移动后的光标 leftcursor 和右光标重合,即 cursor(leftcursor, r) 那么左光标左移之后,就算光标此时判断 cursor 也会兜底,flag = 1。

插入

这个操作是让我们在 p 这个位置插入一个节点,字符为 c,这个就简单一些了,直接对链表 push\_back 一个新节点 {c,{0, 0}},然后像普通链表一样,删除原来两个相邻节点的连接,再分别和这个新节点连接(用other表示另一个光标,在移动 p 前先判断 cursor(other, p) ,如果重合,就一起移)。

删除

这个操作表示删除,和 insert 大致相同,只需要多判断一个是否在链表尾部,和判断另外一个光标的时候多判断一点就行了。

翻转

这个操作让我们实现两个光标之间的翻转,如果 flag = 0 的时候就返回 0,上面解释的比较清楚了,这里就不说了。

主函数:

首先是创建链表,因为边界问题,先创建一个方便操作的节点 {{0, 0}, 0} 和链表头和链表尾,再将头尾 newedge 遍历字符串一个一个 newedge 。然后就是函数调用调用就搞定了。

code

#include<bits/stdc++.h>
using namespace std;
struct node{
    char c;//值
    pair<int,int>lg;//这个点左右两边的下标
};
bool flag=1;//判断左光标是否在右光标左边
string s;
vector<node>lb;//文本
int n;
pair<int,int>leftcursor,rightcursor;//左光标与右光标
inline void newedge(int x,int y){//建边
    if(!lb[x].lg.first)
        lb[x].lg.first=y;
    else
        lb[x].lg.second=y;
    if(!lb[y].lg.first)
        lb[y].lg.first=x;
    else
        lb[y].lg.second=x;
}
inline bool cursor(pair<int,int>a,pair<int,int>b){//光标是否重叠
    return a.first==b.first&&a.second==b.second;
}
inline int nxt(int x,int y){//以 y−x 的方向 x 的下一个下标
    return (lb[x].lg.first==y?lb[x].lg.second:lb[x].lg.first);
}
inline void deleteedge(int x,int y){//删边
    if(lb[x].lg.first==y)
        lb[x].lg.first=0;
    else
        lb[x].lg.second=0;
    if(lb[y].lg.first==x)
        lb[y].lg.first=0;
    else
        lb[y].lg.second=0;
}
int main(){
    freopen("editor.in","r",stdin);
    freopen("editor.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>s>>n;
    //初始化
    lb.reserve(s.size()+n+10);//提前申请空间(要卡常)
    leftcursor={0,1},rightcursor={s.size()-1,s.size()};
    lb.push_back({0,{0,0}});
    lb.push_back({0,{0,0}});
    lb.push_back({0,{0,0}});
    lb.push_back({s[0],{0,0}});
    newedge(1,3);
    for(int i=1;i<s.size();i++){
        lb.push_back({s[i],{0,0}});
        newedge(i+2,i+3);
    }
    newedge(s.size()+2,2);
    leftcursor={1,3},rightcursor={s.size()+2,2};
    //操作
    while(n--){
        char a,b,c;
        cin>>a;
        //右移光标
        if(a=='>'){
            cin>>b;
            if(b=='L'){
                if(leftcursor.second==2){
                    cout<<"F"<<"\n";
                    continue;
                }
                cout<<"T"<<"\n";
                if(cursor(leftcursor,rightcursor))
                    flag=0;
                leftcursor={leftcursor.second,nxt(leftcursor.second,leftcursor.first)};
            }
            else{
                if(rightcursor.second==2){
                    cout<<"F"<<"\n";
                    continue;
                }
                cout<<"T"<<"\n";
                if(cursor(leftcursor,rightcursor))
                    flag=1;
                rightcursor={rightcursor.second,nxt(rightcursor.second,rightcursor.first)};
            }
        }
        //左移光标
        else if(a=='<'){
            cin>>b;
            if(b=='L'){
                if(leftcursor.first==1){
                    cout<<"F"<<"\n";
                    continue;
                }
                cout<<"T"<<"\n";
                if(cursor(leftcursor,rightcursor))
                    flag=1;
                leftcursor={nxt(leftcursor.first,leftcursor.second),leftcursor.first};
            }
            else{
                if(rightcursor.first==1){
                    cout<<"F"<<"\n";
                    continue;
                }
                cout<<"T"<<"\n";
                if(cursor(leftcursor,rightcursor))
                    flag=0;
                rightcursor={nxt(rightcursor.first,rightcursor.second),rightcursor.first};
            }
        }
        //插入字符
        else if(a=='I'){
            cin>>b>>c;
            lb.push_back({c,{0,0}});
            if(b=='L'){
                deleteedge(leftcursor.first,leftcursor.second);
                newedge(leftcursor.first,lb.size()-1);
                newedge(leftcursor.second,lb.size()-1);
                if(cursor(leftcursor,rightcursor))
                    rightcursor.first=lb.size()-1;
                leftcursor.first=lb.size()-1;
            }
            else{
                deleteedge(rightcursor.first,rightcursor.second);
                newedge(rightcursor.first,lb.size()-1);
                newedge(rightcursor.second,lb.size()-1);
                if(cursor(leftcursor,rightcursor))
                    leftcursor.first=lb.size()-1;
                rightcursor.first=lb.size()-1;
            }
            cout<<"T"<<"\n";
        }
        //删除字符
        else if(a=='D'){
            cin>>b;
            if(b=='L'){
                if(leftcursor.second==2){
                    cout<<"F"<<"\n";
                    continue;
                }
                int y=(lb[leftcursor.second].lg.first==leftcursor.first?lb[leftcursor.second].lg.second:lb[leftcursor.second].lg.first);
                deleteedge(leftcursor.second,y);
                deleteedge(leftcursor.first,leftcursor.second);
                newedge(leftcursor.first,y);
                if(cursor(leftcursor,rightcursor))
                    rightcursor.second=y;
                leftcursor.second=y;
                cout<<"T"<<"\n";
            }
            else{
                if(rightcursor.second==2){
                    cout<<"F"<<"\n";
                    continue;
                }
                int y=(lb[rightcursor.second].lg.first==rightcursor.first?lb[rightcursor.second].lg.second:lb[rightcursor.second].lg.first);
                deleteedge(rightcursor.second,y);
                deleteedge(rightcursor.first,rightcursor.second);
                newedge(rightcursor.first,y);
                if(cursor(leftcursor,rightcursor))
                    leftcursor.second=y;
                rightcursor.second=y;
                cout<<"T"<<"\n";
            }
        }
        //翻转
        else if(a=='R'){
            if(!flag||cursor(leftcursor,rightcursor)){
                cout<<"F"<<"\n";
                continue;
            }
            deleteedge(leftcursor.first,leftcursor.second);
            deleteedge(rightcursor.first,rightcursor.second);
            newedge(leftcursor.first,rightcursor.first);
            newedge(leftcursor.second,rightcursor.second);
            swap(leftcursor.second,rightcursor.first);
            cout<<"T"<<"\n";
        }
        //输出文本
        else{
            int t=lb[1].lg.first,yq=1;
            while(t!=2){
                cout<<lb[t].c;
                if(lb[t].lg.first==yq){
                    yq=t;
                    t=lb[t].lg.second;
                }
                else{
                    yq=t;
                    t=lb[t].lg.first;
                }
            }
        }
    }
    return 0;
}