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
  • 相关阅读:
    ege图形化编程配置过程及出现的问题解决方法
    两个头文件相互包含导致未定义类型
    20180318CSP比赛
    jdk7和jdk8都下载了 如何设置java版本为jdk7?
    CCF|CSP|模拟试题|游戏
    2018蓝桥杯|基础练习|十六进制转八进制
    2018蓝桥杯|历届试题|翻硬币
    2018蓝桥杯|历届试题|数字游戏
    2020年9月18日 可变字符序列:StringBuffer和StringBuilder(尽量掌握底层代码跟踪分析的能力)
    2020年9月17日 String 常用方法四、五、六、七、八、九
  • 原文地址:https://www.cnblogs.com/ctyakwf/p/13271982.html
Copyright © 2011-2022 走看看