Folding - ZOJ 1554 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))


题意:折叠相同的字母,使得结果的字符个数最少。注意括号也算字符,数字每一位算一个字符。

思路:对于dp[l][r]只能有三种情况,第一种是它本身,第二种是dp[l][k]+dp[k+1][r],第三种是如果他能够变成n(dp[l][k])的形式。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int dp[110][110],f[110][110],length[110];
ll Hash[110],p[110],MOD=1e9+7;
char s[110];
bool check(int l,int r,int k)
{ ll ret=Hash[l+k-1]-Hash[l-1];
  int q=(r-l+1)/k;
  int i,j;
  bool flag=true;
  for(i=2;i<=q;i++)
  { if(ret*p[k*(i-1)]!= Hash[l+k*i-1]-Hash[l+k*(i-1)-1])
     flag=false;
  }
  return flag;
}
void solve(int l,int r)
{ int k,len=r-l+1;
  dp[l][r]=len;
  f[l][r]=0;
  for(k=l;k<r;k++)
   if(dp[l][k]+dp[k+1][r]<dp[l][r])
   { f[l][r]=k;
     dp[l][r]=dp[l][k]+dp[k+1][r];
   }
  for(k=1;k<=len/2;k++)
   if(len%k==0 && dp[l][l+k-1]+2+length[len/k]<dp[l][r] && check(l,r,k))
   { f[l][r]=1000+k;
     dp[l][r]=dp[l][l+k-1]+2+length[len/k];
   }
}
void print(int l,int r)
{ int i,j,k;
  if(f[l][r]==0)
  { for(i=l;i<=r;i++)
     printf("%c",s[i]);
  }
  else if(f[l][r]/1000==0)
  { print(l,f[l][r]);
    print(f[l][r]+1,r);
  }
  else
  { printf("%d(",(r-l+1)/(f[l][r]-1000));
    print(l,l+f[l][r]-1000-1);
    printf(")");
  }
}
int main()
{ int n,m,i,j,k;
  p[0]=1;
  for(i=1;i<=105;i++)
   p[i]=p[i-1]*MOD;
  for(i=1;i<=9;i++)
   length[i]=1;
  for(i=10;i<=99;i++)
   length[i]=2;
  length[100]=3;
  while(~scanf("%s",s+1))
  { memset(dp,0,sizeof(dp));
    n=strlen(s+1);
    for(i=1;i<=n;i++)
    { dp[i][i]=1;
      f[i][i]=0;
      Hash[i]=Hash[i-1]+s[i]*p[i];
    }
    for(i=n-1;i>=1;i--)
     for(j=i+1;j<=n;j++)
      solve(i,j);
    print(1,n);
    printf("\n");
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值