原题链接
太菜了,又去看了题解((DP)写一道看一道(QAQ)
题解
#include<cstdio>
using namespace std;
const int N = 510;
int a[N], f[N], g[N], l;
inline void re_l()
{
char c = getchar();
l = 0;
for (; c < '0' || c > '9'; c = getchar());
for (; c >= '0' && c <= '9'; c = getchar())
a[++l] = c - '0';
}
inline int maxn(int x, int y)
{
return x > y ? x : y;
}
bool cmp(int x, int y, int xx, int yy)
{
for (; !a[x] && x <= y; x++);
for (; !a[xx] && xx <= yy; xx++);
int z = y - x + 1, zz = yy - xx + 1;
if (!z || !zz || z < zz)
return false;
if (z > zz)
return true;
for (; x <= y; x++, xx++)
{
if (a[x] > a[xx])
return true;
else
if (a[x] < a[xx])
return false;
}
return false;
}
int main()
{
int i, j, nw = 1;
bool p = 0;
re_l();
for (i = 1; i <= l; i++)
{
for (j = i; j; j--)
if (cmp(j, i, f[j - 1], j - 1))
break;
f[i] = maxn(1, j);
}
g[f[l]] = l;
for (i = f[l]; !a[i - 1]; g[--i] = l);
for (i = f[l] - 1; i; i--)
{
for (j = f[l] - 1; j >= i; j--)
if (cmp(j + 1, g[j + 1], i, j))
break;
g[i] = maxn(maxn(g[i], i), j);
}
for (; nw <= l; nw = g[nw] + 1)
{
if (p)
putchar(',');
p = 1;
for (i = nw; i <= g[nw]; i++)
printf("%d", a[i]);
}
return 0;
}