AcWing 319. Folding sequence (interval dp)

Topic portal

The meaning of the question: give you a string with a length of no more than 100, we can fold it, the folding rules are as follows: If there is a string repeated multiple times, such as ababab abababa b a b a b , it can be changed to3 (ab) 3(ab)3(ab) a a a a a a aaaaaa a a a a a a can be changed to6 (a) 6(a)6(a), a b c c c a b c c c abcccabccc a b c c c a b c c c can be changed to2 (abccc) 2(abccc)2 ( a b c c c ) or2 (ab 3 (c)) 2(ab3(c))2 ( a b 3 ( c ) ) . Ask you the minimum length string that can be folded into a given string.

Idea: To find the shortest length of the string, we can first find its length, see this question for details . Here we mainly solve the output problem.
Now that we know the shortest length that we can fold, then we have to [1, n] [1,n][1,n ] Go deep search.

如果
f [i] [j] = j - i + 1 f [i] [j] = j-i + 1f[i][j]=ji+1. If there is no fold in this paragraph or the length does not decrease after folding, we can directly output this paragraph.
Otherwise,
we will restore the folding process. Enumerate a k from l to r, if it is found thatf [i] [j] = f [i] [k] + f [k + 1] [j] f[i][j]=f[i][k ]+f[k+1][j]f[i][j]=f[i][k]+f[k+1 ] [ j ] , thenf [i] [j] f[i][j]f[i][j]就是 f [ i ] [ k ] f[i][k] f [ i ] [ k ] andf [k + 1] [j] f[k+1][j]f[k+1 ] [ j ] spliced ​​together, then directly search for[i, k] [i,k][i,k], [ k + 1 , j ] [k+1,j] [k+1,j ] .
There is
also the problem of folding, we also need to judge xs [i... K] s[i...k]S [ I . . . K ] Can instead ofs [k + 1 ... j]s [ k+1...j]

Code:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define pii pair<int,int>
#define ull unsigned long long
#define pdd pair<double,double>
#define lowbit(x) x&-x
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read()
{
    
    
    int x=0,f=1;
    char ch=gc();
    while(ch<'0'||ch>'9')
    {
    
    
        if(ch=='-')
            f=-1;
        ch=gc();
    }
    while(ch>='0'&&ch<='9')
    {
    
    
        x=x*10+ch-'0';
        ch=gc();
    }
    return x*f;
}
using namespace std;
const int N=2e4+55;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1);
char s[N];
int f[222][222],b[222];
bool canfold(int i,int j,int k)
{
    
    
    int len=j-i+1,p=k-i+1;
    if(len%p)
        return 0;
    int l=i,r=k+1;
    while(r<=j)
    {
    
    
        if(s[l]==s[r])
        {
    
    
            l++;r++;
        }
        else return 0;
    }
    return 1;
}
void dfs(int l,int r)
{
    
    
    if(f[l][r]==r-l+1)
    {
    
    
        for(int i=l;i<=r;i++)
            cout<<s[i];
        return ;
    }
    for(int k=l;k<r;k++)
    {
    
    
        if(f[l][r]==f[l][k]+f[k+1][r])
        {
    
    
            dfs(l,k);dfs(k+1,r);
            return ;
        }
    }
    for(int k=l;k<=r;k++)
    {
    
    
        if(canfold(l,r,k))
        {
    
    
            cout<<(r-l+1)/(k-l+1);
            cout<<'(';
            dfs(l,k);
            cout<<')';
            return ;
        }
    }
}
void solve()
{
    
    
    cin>>s+1;
    int n=strlen(s+1);
    for(int i=1;i<10;i++) b[i]=1;
    for(int i=10;i<100;i++) b[i]=2;
    b[100]=3;
    for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++)
            f[i][j]=j-i+1;
    for(int len=2;len<=n;len++)
    {
    
    
        for(int i=1;i+len-1<=n;i++)
        {
    
    
            int j=i+len-1;
            for(int k=i;k<j;k++)
            {
    
    
                f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]);
                if(canfold(i,j,k))
                {
    
    
                    int cnt=(len/(k-i+1));
                    f[i][j]=min(f[i][j],b[cnt]+f[i][k]+2);
                }
            }
        }
    }
    dfs(1,n);
}
signed main()
{
    
    
//    int t;
//    cin>>t;
//    while(t--)
        solve();
    return 0;
}


Guess you like

Origin blog.csdn.net/Joker_He/article/details/109616420