ZOJ 1554 Folding(区间dp)

Folding

Time Limit: 10 Seconds      Memory Limit: 32768 KB      Special Judge

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

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

Process to the end of input.


Output

Write to the output file 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))


Source: Northeastern Europe 2002
Submit    Status


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-12

typedef long long ll;

using namespace std;

#define INF 0x3f3f3f3f
#define N 105

string s;
int dp[N][N];
string f[N][N];

int judge(int le,int ri)
{
    int i,j;
    int len=(ri-le+1);
    for(i=1;i<=len/2;i++)
    {
        bool flag=false;
        if(len%i) continue;
        for(j=le;j+i<=ri;j++)
           if(s[j]!=s[j+i]) {flag=true; break;}
        if(!flag) return i;
    }
    return 0;
}

int dfs(int le,int ri)
{
    if(dp[le][ri]!=-1) return dp[le][ri];
    if(le==ri)
    {
        f[le][ri]=s[le];
        dp[le][ri]=1;
        return 1;
    }
    int mid,i;
    dp[le][ri]=ri-le+1+1;

    for(i=le;i<ri;i++)
    {
       int he=dfs(le,i)+dfs(i+1,ri);
       if(he<dp[le][ri])
       {
           dp[le][ri]=he;
           mid=i;
       }
    }
    f[le][ri]=f[le][mid]+f[mid+1][ri];

    int len=judge(le,ri);

    if(len)
    {
        int tt=(ri-le+1)/len;
        char c[100];
        sprintf(c,"%d",tt);
        string temp="";
        temp=c;
        temp=temp+"("+f[le][le+len-1]+")";
        if(temp.length()<dp[le][ri])
        {
            dp[le][ri]=temp.length();
            f[le][ri]=temp;
        }
    }
    return dp[le][ri];
}
int main()
{
    int i,j;
    while(cin>>s)
    {
        int len=s.length();
        memset(dp,-1,sizeof(dp));
        dfs(0,len-1);
        cout<<f[0][len-1]<<endl;
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值