[题解?]luogu_P1415拆分数列(dp(不懂

不知道题解啥意思咋想到的

#include<bits/stdc++.h>
using namespace std;
const int maxn=509;
int n,f[maxn],f2[maxn];
char ch[maxn];
int t[maxn],t2[maxn];
inline int cmp(int a,int b,int c,int d){
    if(d==0)return 1;
    memset(t,0,sizeof(t));memset(t2,0,sizeof(t2));
    int l1=0,l2=0;
    for(int i=b;i>=a;i--)t[++l1]=ch[i];
    for(int i=d;i>=c;i--)t2[++l2]=ch[i];
    l1=max(l1,l2);
    for(int i=l1;i;i--)
    if(t[i]!=t2[i])return t[i]>t2[i];
    return -1;
}
int main(){
    scanf("%s",ch+1);n=strlen(ch+1);
    for(int i=1;i<=n;i++)ch[i]-='0';
    for(int i=1;i<=n;i++){
        f[i]=1;
        for(int j=i;j>=1;j--)
        if(cmp(j,i,f[j-1],j-1)==1){
            f[i]=max(f[i],j);break;
        }
    }
    f2[f[n]]=n;
    int cnt=0;
    for(int i=f[n]-1;i&&!ch[i];i--)f2[i]=n,++cnt;//前导0 
    for(int i=f[n]-1-cnt;i>=1;i--){
        f2[i]=i;
        for(int j=f[n]-1;j>=i;j--)
        if(cmp(i,j,j+1,f2[j+1])==0){
            f2[i]=max(f2[i],j);break;
        }
    }
    int pos=1;bool fl=1;
    while(pos<=n){
        if(fl)fl=0;
        else putchar(',');
        for(int i=pos;i<=f2[pos];i++)putchar(ch[i]+'0');
        pos=f2[pos]+1;
    }
}

猜你喜欢

转载自www.cnblogs.com/superminivan/p/11673861.html