CSP-S 2023 题解

· · 个人记录

T1 密码锁

略

T3 结构体

考场上写了线段树。本意是因为我觉得地址太大,所以用线段树查找地址和元素更快一些。因为要递归,我还在结构体里写了线段树,简单来说就是树套树。但是我并没有注意到 n \leq 100 ,也对自己这么弱的码力没有一点概念。

正解大模拟 O(n^2k\log n) (我认为线段树优化到 O(n^2 \log k \log n) 可以保留)

打模拟的时候要时刻清楚自己要做什么:这个变量和数组表示什么,这层循环要做什么,这个if条件之下要做什么。

注意,要考虑内存中元素间的空格,对应代码中判断 x<0。注意细节

#include<bits/stdc++.h>
using namespace std;

const int N=1e2+10;
typedef long long ll;
int n,tot,cnt;ll addr;//tot of structs;cnt of elements
map<string,int> mp_s,mp_e;
struct Struct{
    ll siz,tot_siz;
    string name;
    vector<int> mem;
    vector<string> mem_name;
    vector<ll> pre_siz;
}a[N];
struct Element{
    int type;ll addr;
    string name;
}b[N];
void init(){
    addr = 0;
    mp_s["byte"] = ++tot;
    a[tot].name = "byte";
    a[tot].siz = a[tot].tot_siz =1;
    a[tot].pre_siz.push_back(1);
    mp_s["short"] = ++tot;
    a[tot].name = "short";
    a[tot].siz = a[tot].tot_siz =2;
    a[tot].pre_siz.push_back(2); 
    mp_s["int"] = ++tot;
    a[tot].name = "int";
    a[tot].siz = a[tot].tot_siz =4;
    a[tot].pre_siz.push_back(4); 
    mp_s["long"] = ++tot;
    a[tot].name = "long";
    a[tot].siz = a[tot].tot_siz =8;
    a[tot].pre_siz.push_back(8); 
}
void solve1(){
    string s;int k;
    cin>>s>>k;
    mp_s[s] = ++tot;
    a[tot].name = s;
    ll tmp=0;
    for(int i=1;i<=k;++i){
        string t,n;
        cin>>t>>n;
        int j=mp_s[t];
        a[tot].mem.push_back(j);
        a[tot].mem_name.push_back(n);
        a[tot].siz = max(a[tot].siz,a[j].siz);
        tmp = tmp?((tmp-1)/a[j].siz+1)*a[j].siz:0;
        tmp += a[j].tot_siz;
        a[tot].pre_siz.push_back(tmp);
    }
    a[tot].tot_siz = ((a[tot].pre_siz.back()-1)/a[tot].siz+1)*a[tot].siz;
    printf("%lld %d\n",a[tot].tot_siz,a[tot].siz);
}
void solve2(){
    string t,n;
    cin>>t>>n;
    mp_e[n] = ++cnt;
    b[cnt].type = mp_s[t];
    int p=b[cnt].type;
//  addr = addr-addr%a[p].siz+a[p].siz;
    addr = addr?((addr-1)/a[p].siz+1)*a[p].siz:0;
    //0 1 2 3 4 5 6 7 8
    //1 2 3 4 5 6 7 8 9 
    b[cnt].addr = addr;
    addr += a[p].tot_siz;
    b[cnt].name = n;
    printf("%lld\n",b[cnt].addr);
}
void solve3(){
    string s;
    cin>>s;
    s += '.';
    queue<string> q;
    for(int i=0,j=0;i<s.size();++i)
        if(s[i]=='.'){
            q.push(s.substr(j,i-j));
            j = i+1; 
        }
    ll ans=0;
    int p,flag=1;
    while(!q.empty()){
        string now=q.front();
        q.pop();
        if(flag){
            p = mp_e[now];
            ans += b[p].addr;
            flag = 0;
            p = b[p].type;
        }
        else{
            for(int i=0;i<a[p].mem.size();++i)
                if(a[p].mem_name[i]==now){
                    int j=a[p].mem[i];
                    ans += a[p].pre_siz[i]-a[j].tot_siz;
                    p = j;
                    break;
                }
        }
    }
    printf("%lld\n",ans); 
}
void solve4(){
    ll x;
    cin>>x;
    ll y=x;
    string res="";
    bool flag=0;
    int p;
    for(int i=1;i<=cnt;++i){
        int j=b[i].type;
        if(x<b[i].addr+a[j].tot_siz){//**?? +1 考虑空格??!!!!!! 元素间空格 
            // x x+1 x+2 (siz=3)
            x -= b[i].addr;
            res += b[i].name;
            p = b[i].type;
            flag = 1;
            break;
        }
    }
    if(x<0||!flag){
        printf("ERR\n");
        return;
    }
    while(p>4){
        for(int i=0;i<a[p].mem.size();++i){
            if(i==a[p].mem.size()-1||x<a[p].pre_siz[i+1]-a[a[p].mem[i+1]].tot_siz){
                int j=a[p].mem[i];
                if(x>=a[p].pre_siz[i]){
                    printf("ERR\n");
                    return;
                }
                else{
                    x -= a[p].pre_siz[i]-a[j].tot_siz;
                    res += '.';
                    res += a[p].mem_name[i];
                    p = a[p].mem[i];
                    break;
                }
            }
        }
    }
    cout<<res<<endl;
}
signed main(){
    cin>>n;
    init();
    for(int i=1;i<=n;++i){
        int op;
        cin>>op;
        if(op==1)solve1();
        else if(op==2)solve2();
        else if(op==3)solve3();
        else solve4();
    }
    return 0;
}