宁波多校(三) H rabster的字符串世界(瞎搞题)

可以不用建字典树

其实字典树的新增节点大小就是排序后的字符串数组按序与前一个的最大后缀

但是本题的每个字符不只一位并且卡了点内存,考虑建立结构体存储

插入和删除可以维护一个set来找位置,算法比较简单,代码很蠢

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int ans;
struct node{
    short a[3001];
    short length;
    void init(){
        memset(this->a,-1,sizeof(this->a));
        length=0;
    }
    friend bool operator <(const node &x,const node &y){
        short len=max(x.length,y.length);
        for(short i=1;i<=len;i++){
            if(x.a[i]!=y.a[i])
                return x.a[i]<y.a[i];
        }
        return false;
    }
    friend bool operator ==(const node &x,const node &y){
        if(x.length!=y.length)
            return false;
        for(short i=1;i<=x.length;i++){
            if(x.a[i]!=y.a[i])
                return false;
        }
        return true;
    }
}s[3001];
struct cmp{
    bool operator()(short x,short y)
    {
        if(!(s[x]==s[y]))
            return s[x]<s[y];
        else
            return x<y;
    }
};
set<short,cmp> se;
short cal(short i,short j){
    auto a=s[i];
    auto b=s[j];
    short cnt=0;
    for(short l=1,r=1;;l++,r++){
        if(l>a.length||r>b.length)
            break;
        if(a.a[l]!=b.a[r])
            break;
        if(a.a[l]==b.a[r])
            cnt++;
    }
    return cnt;
}
void earse(short pos){
    auto a=s[pos];
    auto tmp=se.find(pos);
    auto ed=se.end();
    ed--;
    if(tmp==se.begin()){
        ans-=a.length;
        auto b=++tmp;
        short scnt=cal(pos,*b);
        ans+=scnt;
    }
    else if(tmp==ed){
        ans-=a.length;
        auto b=--tmp;
        short scnt=cal(pos,*b);
        ans+=scnt;
    }
    else{
        auto x=tmp;
        auto c=--x;
        tmp++;
        short scnt=cal(pos,*c);
        short tcnt=cal(pos,*tmp);
        ans-=(s[pos].length-max(scnt,tcnt));
    }
    se.erase(pos);
}
void insert(short pos){
    auto a=s[pos];
    auto tmp=se.lower_bound(pos);
    auto ed=se.end();
    ed--;
    if(tmp==se.begin()){
        short tcnt=cal(pos,*tmp);
        ans+=a.length-tcnt;
    }
    else if(tmp==se.end()){
        tmp--;
        short tcnt=cal(pos,*tmp);
        ans+=a.length-tcnt;
    }
    else{
        auto tmp3=tmp;
        tmp3--;
        short tcnt=cal(pos,*tmp);
        short scnt=cal(pos,*tmp3);
        ans+=a.length-max(scnt,tcnt);
    }
    se.insert(pos);
}
int main(){
   // freopen("8.in","r",stdin);
    ios::sync_with_stdio(false);
    short n;
    cin>>n;
    short i;
    for(i=1;i<=n;i++){
        short l;
        cin>>l;
        for(short j=1;j<=l;j++){
            cin>>s[i].a[j];
        }
        s[i].length=l;
        se.insert(i);
    }
    ans=0;
    auto it=se.begin();
    auto it1=it;
    it++;
    ans+=s[*it1].length;
    for(;it!=se.end();it++){
        short scnt=cal(*it,*it1);
        ans+=s[*it].length-scnt;
        it1=it;
    }
    cout<<ans<<endl;
    short q;
    cin>>q;
    while(q--){
        short l;
        short pos;
        cin>>pos;
        earse(pos);
        cin>>l;
        s[pos].init();
        for(i=1;i<=l;i++){
            cin>>s[pos].a[i];
        }
        s[pos].length=l;
        insert(pos);
        cout<<ans<<endl;
    }
}
View Code

 

posted @ 2020-07-09 10:13  朝暮不思  阅读(150)  评论(0编辑  收藏  举报