非常牛的题

· · 题解

考虑将询问的串分段,每一段确定一个数。

#include <bits/stdc++.h>
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
/*
孤独摇滚!
好看
by qqqaaazzz

wow !
CF212

2 3 1 5 4
0 0 1 1 0

2 3 9 5 6



0 0 1 0 1

5
2 3 1 5 4

6
2 3 4 1 5 6

0->1

112131)2)()))))))
*/
string anss[500010];
int len[500010];
int f[500010][23];//f[i][j]表示走到 i,用了 j 步,每次走值要翻倍,最小结果? 
int lst[500010][23];

int fun(string s){
    //求!
    int ans = 0;
    int n = s.length();
    for (int i=0;i<n;i++){
        int cnt = 0;
        for (int j=i;j<n;j++){
            if(s[j]=='(') ++cnt;
            else --cnt;
            if(cnt<0) break;
            if(cnt==0) ++ans;
        }
    }
    return ans;
}
int cha(string cur){
    string to;
    for (auto i : cur) to += (i=='('? '(':')');
    int res = fun(to);
    to = "";
    for (auto i : cur) to += (i==')'? ')':'(');
    return fun(to)-res;
}
void print(int i,int j){
    if(i==0) return;
    if(lst[i][j]!=-1){
        cout << i << " " << anss[i] << "\n";
        print(lst[i][j],j-1);
    }
    else{
        print(i-1,j);
    }
}

signed main()
{
    for (int i=0;i<=200;i++){
        for (int j=0;j<=200;j++){
            string qaq;
            for (int k=1;k<=i;k++) qaq += "()";
            qaq += "1)";
            for (int k=1;k<=j;k++) qaq += "()";
            if(anss[cha(qaq)].empty()||qaq.length()<anss[cha(qaq)].length()){
                anss[cha(qaq)] = qaq;
            }
        }
    }
    for (int i=1;i<=50000;i++){
        if(anss[i].length()==0) len[i] = 1e9;
        else len[i] = anss[i].length()+1;
    }
    memset(f,0x3f,sizeof(f));
    f[0][0] = 0;

    for (int i=1;i<=5000;i++){
        for (int j=1;j<=20;j++){
            //f[i][j]从 f[k][j-1] 转移过来,k<i/2,要加上 len[i]
            lst[i][j] = -1;
            for (int k=0;k<=i/2;k++){
                f[i][j] = min(f[i][j],f[k][j-1]+len[i]);
                if(f[k][j-1]+len[i]==f[i][j]) lst[i][j] = k;
            }
            f[i][j] = min(f[i][j],f[i-1][j]);
        }
    }
    print(5000,13);
    return 0;
}

得到结果:

5000 ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()1)()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()
2500 ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()1)()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()
1250 ()()()()()()()()()()()()()()()()()()()()()()()()1)()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()
625 ()()()()()()()()()()()()()()()()()()()()()()()()1)()()()()()()()()()()()()()()()()()()()()()()()()
312 ()()()()()()()()()()()()1)()()()()()()()()()()()()()()()()()()()()()()()
156 ()()()()()()()()()()()1)()()()()()()()()()()()()
78 ()()()()()1)()()()()()()()()()()()()
39 ()()1)()()()()()()()()()()()()
19 1)()()()()()()()()()()()()()()()()()()
9 ()()1)()()
4 ()1)()
2 1)()
1 1)

这个代码方便调试:

#include <bits/stdc++.h>
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
/*
孤独摇滚!
好看
by qqqaaazzz
*/
int n,m;
char s[500010];

int f(string s){
    //求!
    int ans = 0;
    int n = s.length();
    for (int i=0;i<n;i++){
        int cnt = 0;
        for (int j=i;j<n;j++){
            if(s[j]=='(') ++cnt;
            else --cnt;
            if(cnt<0) break;
            if(cnt==0){
                ++ans;
                cout << i << " " << j << "\n";
            }
        }
    }
    return ans;
}

signed main()
{
    cin >> s+1;
    n = strlen(s+1);
    cin >> m;
    string res;
    for (int i=1;i<=m;i++){
        int x;
        cin >> x;
        res += s[x];
    }
    cout << res << "\n";
    cout << f(res) << "\n";
    return 0;
}

another code :

#include <bits/stdc++.h>
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
/*
孤独摇滚!
好看
by qqqaaazzz

wow !
CF212

2 3 1 5 4
0 0 1 1 0

2 3 9 5 6



0 0 1 0 1

5
2 3 1 5 4

6
2 3 4 1 5 6

0->1

112131)2)()))))))
*/
string anss[500010];
int len[500010];
int f[500010][23];//f[i][j]表示走到 i,用了 j 步,每次走值要翻倍,最小结果? 
int lst[500010][23];

int fun(string s){
    //求!
    int ans = 0;
    int n = s.length();
    for (int i=0;i<n;i++){
        int cnt = 0;
        for (int j=i;j<n;j++){
            if(s[j]=='(') ++cnt;
            else --cnt;
            if(cnt<0) break;
            if(cnt==0) ++ans;
        }
    }
    return ans;
}
int cha(string cur){
    string to;
    for (auto i : cur) to += (i=='('? '(':')');
    int res = fun(to);
    to = "";
    for (auto i : cur) to += (i==')'? ')':'(');
    return fun(to)-res;
}
int F;
void print(int i,int j){
    if(i==0) return;
    if(lst[i][j]!=-1){
        cout << "{" << i << ",\"" << anss[i] << "\"},";
        F += len[i];
        print(lst[i][j],j-1);
    }
    else{
        print(i-1,j);
    }
}

signed main()
{
    for (int i=0;i<=200;i++){
        for (int j=0;j<=200;j++){
            string qaq;
            for (int k=1;k<=i;k++) qaq += "()";
            qaq += "1)";
            for (int k=1;k<=j;k++) qaq += "()";
            if(anss[cha(qaq)].empty()||qaq.length()<anss[cha(qaq)].length()){
                anss[cha(qaq)] = qaq;
            }
        }
    }
    for (int i=0;i<=200;i++){
        for (int j=0;j<=200;j++){
            string qaq;
            for (int k=1;k<=i;k++) qaq += "1)";
            qaq += "1)";
            for (int k=1;k<=j;k++) qaq += "1)";
            if(anss[cha(qaq)].empty()||qaq.length()<anss[cha(qaq)].length()){
                anss[cha(qaq)] = qaq;
            }
        }
    }
    for (int i=1;i<=50000;i++){
        if(anss[i].length()==0) len[i] = 1e9;
        else len[i] = anss[i].length()+1;
    }
    memset(f,0x3f,sizeof(f));
    f[0][0] = 0;

    for (int i=1;i<=10000;i++){
        for (int j=1;j<=20;j++){
            //f[i][j]从 f[k][j-1] 转移过来,k<i/2,要加上 len[i]
            for (int k=0;k<=i/2;k++){
                f[i][j] = min(f[i][j],f[k][j-1]+len[i]);
                if(f[k][j-1]+len[i]==f[i][j]) lst[i][j] = k;
            }
            f[i][j] = min(f[i][j],f[i-1][j]);
            if(f[i-1][j]==f[i][j]) lst[i][j] = -1;
        }
//      for (int j=1;j<=20;j++) if(f[i][j]<1e9) cout << f[i][j] << " ";
//      cout << "\n";
    }
    cout << f[10000][14] << "\n";
    print(10000,14);
    return 0;
}