poj2176 Folding

poj2176 题 目 传 送 门 p o j 2176


题目

Folding F o l d i n g

TimeLimit:1000MS T i m e L i m i t : 1000 M S
MemoryLimit:65536K M e m o r y L i m i t : 65536 K
TotalSubmissions:1715 T o t a l S u b m i s s i o n s : 1715
Accepted:617 A c c e p t e d : 617
SpecialJudge S p e c i a l J u d g e

Description D e s c r i p t i o n

Bill is trying to compactly represent sequences of capital alphabetic characters from ‘A’ to ‘Z’ by folding repeating subsequences inside them. For example, one way to represent a sequence AAAAAAAAAABABABCCD is 10(A)2(BA)B2(C)D. He formally defines folded sequences of characters along with the unfolding transformation for them in the following way:
A sequence that contains a single character from ‘A’ to ‘Z’ is considered to be a folded sequence. Unfolding of this sequence produces the same sequence of a single character itself.
If S and Q are folded sequences, then SQ is also a folded sequence. If S unfolds to S’ and Q unfolds to Q’, then SQ unfolds to S’Q’.
If S is a folded sequence, then X(S) is also a folded sequence, where X is a decimal representation of an integer number greater than 1. If S unfolds to S’, then X(S) unfolds to S’ repeated X times.

According to this definition it is easy to unfold any given folded sequence. However, Bill is much more interested in the reverse transformation. He wants to fold the given sequence in such a way that the resulting folded sequence contains the least possible number of characters.

Input I n p u t

The input contains a single line of characters from ‘A’ to ‘Z’ with at least 1 and at most 100 characters.

Output O u t p u t

Write to the output a single line that contains the shortest possible folded sequence that unfolds to the sequence that is given in the input file. If there are many such sequences then write any one of them.

SampleInput S a m p l e I n p u t

AAAAAAAAAABABABCCD

SampleOutput S a m p l e O u t p u t

9(A)3(AB)CCD


题解

看样例猜题意:折叠字符串,使长度最短。
这是一道很简单的区间DP。设 f[i][j] f [ i ] [ j ] 表示折叠后的最短字串, g[i][j] g [ i ] [ j ] 表示 f[i][j] f [ i ] [ j ] 的长度。 f[i][j] f [ i ] [ j ] 可以直接折叠,也可以合并两段。
由于贪心,我们可以发现:折叠次数越大,长度越短。我们可以枚举折叠次数,直接转移。
所以,这题就简单地被解决了。


标程

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 150;
char s[N], r[N];
char f[N][N][N];
int g[N][N];

void fun(int a, int b) {
    if (a == b) {
        f[a][b][0] = s[a];
        g[a][b] = 1;
        return;
    }
    for (int i = 1; i <= b - a; i++) {
        if ((b - a + 1) % i) continue;
        bool p = 1;
        for (int j = a + i; j <= b; j++) {
            if (s[j] == s[j - i]) continue;
            p = 0; break;
        }
        if (!p) continue;
        if (!g[a][a + i - 1]) fun(a, a + i - 1);
        sprintf(r, "%d", (b - a + 1) / i);
        strcpy(f[a][b], r);
        g[a][b] = strlen(f[a][b]);
        f[a][b][g[a][b]++] = '(';
        strcat(f[a][b], f[a][a + i - 1]);
        g[a][b] += g[a][a + i - 1];
        f[a][b][g[a][b]++] = ')';
        break;
    }
    for (int i = a; i < b; i++) {
        if (!g[a][i]) fun(a, i);
        if (!g[i + 1][b]) fun(i + 1, b);
        if (!g[a][b] || g[a][i] + g[i + 1][b] < g[a][b]) {
            strcpy(f[a][b], f[a][i]);
            strcat(f[a][b], f[i + 1][b]);
            g[a][b] = g[a][i] + g[i + 1][b];
        }
    }
}

int main() {
    scanf("%s", s);
    int l = strlen(s);
    fun(0, l - 1);
    printf("%s\n", f[0][l - 1]);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值