uva 1630 Folding

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, thenSQ 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. IfS unfolds to S', thenX(S) unfolds to S' repeatedX 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 

Input file contains several test cases, one per line. Each of them contains a single line of characters from `A' to `Z' with at least 1 and at most 100 characters.

Output 

For each input case write a different output line. This must be 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.

Sample Input 

AAAAAAAAAABABABCCD
NEERCYESYESYESNEERCYESYESYES

Sample Output 

9(A)3(AB)CCD
2(NEERC3(YES)) 

这题目的思路就是看当前的字符串是否可以折叠,如果可以就看折叠后的长度时候比之前的长度要小,用dp[i][j]表示字符串从i~j的经过折叠后的结果,如果不可以折叠,就搜寻最优的切割k使得dp[i][k].size()+dp[k+1][j].size()最小,再把结果保存下来就可以。也可以算的上是一道记忆搜索题目。

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 105;
string tmp;
string dp[maxn][maxn];

int isfold (int i , int j) {
    int len = j-i+1;
    for (int k = 1 ; k <= len/2 ; k++) {
        if (len%k)
            continue;
        int flag = 0;
        for (int c = 0 ; c < len/k-1 ; c++) {
            for (int s = 0 ; s < k ; s++) {
                if (tmp[c*k+s+i] != tmp[c*k+k+s+i])
                    flag = 1;
            }
        }
        if (!flag)
            return len/k;
    }
    return -1;
}

int main () {
    while (cin >> tmp) {
        int len = tmp.size();
        for (int l = 1 ; l <= len ; l++) {
            for (int i = 0 ; i <= len-l ; i++) {
                int j = i+l-1;
                dp[i][j] = tmp.substr(i,j+1-i);
                int cnt = isfold(i,j);
                if (cnt > 0) {
                    char num[10];
                    sprintf(num,"%d",cnt);
                    string tn = string(num);
                    if (strlen(num)+2+(j-i+1)/cnt < j-i+1)
                        dp[i][j] = tn+"("+dp[i][i+((j-i+1)/cnt)-1]+")";
                }else {
                    int index = -1;
                    int length = j-i+1;
                    for (int k = i ; k <= j-1 ; k++)
                        if (length > dp[i][k].size()+dp[k+1][j].size()) {
                            index = k;
                            length = dp[i][k].size()+dp[k+1][j].size();
                        }
                    if (index != -1)
                        dp[i][j] = dp[i][index]+dp[index+1][j];
                }
            }
        }
        cout << dp[0][len-1] << endl;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值