zoukankan      html  css  js  c++  java
  • 宁波多校(三) 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
  • 相关阅读:
    <a>與<link>的區別
    103_linux如何安装mysql数据库?
    102_Centos7在虚拟机上开机时出现黑屏,怎么解决?
    101_war和war exploded的区别
    100_linux中passwd文件的用户解析
    099_linux基础命令三
    098_linux基本操作命令二
    097_linux如何配置jdk路径
    096_如何将linux的IP地址转换成静态ip地址?
    078_ip地址.DNS,子网掩码,网关分别是什么,又有什么作用?
  • 原文地址:https://www.cnblogs.com/ctyakwf/p/13271982.html
Copyright © 2011-2022 走看看