Submission #567786

#TimeUsernameProblemLanguageResultExecution timeMemory
567786tqbfjotldCopy and Paste 3 (JOI22_copypaste3)C++14
62 / 100
2021 ms2097152 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

char arr[2505];
int A,B,C;
int mem[2505*1505];
int id[2505][2505];
int ct = 1;

struct node{
    int nid, len;
    vector<int> vals;
    node*ch[26];
    vector<int> decomp[15];
    node(int _len){
        nid = ct++;
        len = _len;
        for (int x = 0; x<26; x++){
            ch[x] = 0;
        }
    }
    void construct(){
        //printf("call construct len %lld\n",len);
        for (int x = 0; x<vals.size(); x++){
            decomp[0].push_back(lower_bound(vals.begin(),vals.end(),vals[x]+len)-vals.begin());
        }
        decomp[0].push_back(vals.size());
        for (int x = 1; x<15; x++){
            for (int y = 0; y<=vals.size(); y++){
                decomp[x].push_back(decomp[x-1][decomp[x-1][y]]);
            }
        }
    }
    void constructall(){
        construct();
        for (int x = 0; x<26; x++){
            if (ch[x]!=0){
                ch[x]->constructall();
            }
        }
    }
    int cntocc(int st, int en){
        //printf("call cntocc %lld %lld, len %lld\n",st,en,len);
        int cur = lower_bound(vals.begin(),vals.end(),st)-vals.begin();
        int ans = 1;
        int r = upper_bound(vals.begin(),vals.end(),en-len)-vals.begin();
        for (int x = 14; x>=0; x--){
            if (decomp[x][cur]<r){
                ans += (1<<x);
                cur = decomp[x][cur];
            }
        }
        return ans;
    }
}*rt;

node* pt[2505][2505];

int cntocc(int a, int b, int st, int en){
    return pt[a][b]->cntocc(st,en);
}


int func(int a, int b){
    if (mem[id[a][b]]!=-1) return mem[id[a][b]];
    if (a==b) return 0;
    int ans = func(a+1,b)+A;
    for (int x = 1; x<b-a; x++){
        int t = func(a,a+x);
        int t2 = cntocc(a,a+x,a,b);
        ans = min(ans,t+B+t2*C+(b-a-x*t2)*A);
        if (t2==1) break;
    }
    //printf("func %lld %lld = %lld\n",a,b,ans);
    return mem[id[a][b]] = ans;
}

main(){
    int n;
    scanf("%lld",&n);
    for (int x = 0; x<n; x++){
        scanf(" %c",&arr[x]);
    }
    scanf("%lld%lld%lld",&A,&B,&C);
    memset(mem,-1,sizeof(mem));
    for (int x = 0; x<n; x++){
        if (rt==0){
            rt = new node(0);
        }
        node *cur = rt;
        for (int y = x+1; y<=n; y++){
            if (cur->ch[arr[y-1]-'a']==0){
                cur->ch[arr[y-1]-'a'] = new node(y-x);
            }
            cur = cur->ch[arr[y-1]-'a'];
            cur->vals.push_back(x);
            pt[x][y] = cur;
            id[x][y] = cur->nid;
        }
    }
    rt->constructall();
    printf("%lld",func(0,n));
}

Compilation message (stderr)

copypaste3.cpp: In member function 'void node::construct()':
copypaste3.cpp:25:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |         for (int x = 0; x<vals.size(); x++){
      |                         ~^~~~~~~~~~~~
copypaste3.cpp:30:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |             for (int y = 0; y<=vals.size(); y++){
      |                             ~^~~~~~~~~~~~~
copypaste3.cpp: At global scope:
copypaste3.cpp:79:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   79 | main(){
      | ^~~~
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |     scanf("%lld",&n);
      |     ~~~~~^~~~~~~~~~~
copypaste3.cpp:83:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |         scanf(" %c",&arr[x]);
      |         ~~~~~^~~~~~~~~~~~~~~
copypaste3.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |     scanf("%lld%lld%lld",&A,&B,&C);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...