Codeforces Gym 100002 Problem F "Folding" 区间DP

Problem F "Folding"

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100002

Description

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.

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

Sample Output

9(A)3(AB)CCD

HINT

 

题意

就是相同的可以被压缩,问你最少压缩成什么样子(注意,括号和数字也算长度

题解:

区间DP,维护区间DP的时候,同时维护一下这个区间的字符串是什么样子的就好了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 110000
#define mod 10007
#define eps 1e-9
#define pi 3.1415926
int Num;
//const int inf=0x7fffffff;   //§ß§é§à§é¨f§³
const ll Inf=0x3f3f3f3f3f3f3f3fll;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************

string s[110][110];
int n;
int dp[110][110];
int vis[110][110];
char S[110];
int FF(int x)
{
    int add=0;
    while(x)
    {
        add++;
        x/=10;
    }
    return add;
}
string F(int x)
{
    string ss;
    while(x)
    {
        ss+=(char)(x%10+'0');
        x/=10;
    }
    reverse(ss.begin(),ss.end());
    return ss;
}
int solve(int l,int r)
{
    if(vis[l][r])return dp[l][r];
    vis[l][r]=1;
    for(int i=l;i<r;i++)
    {
        if(dp[l][r]>solve(l,i)+solve(i+1,r))
        {
            dp[l][r]=dp[l][i]+dp[i+1][r];
            s[l][r]=s[l][i]+s[i+1][r];
        }
    }
    for(int i=1;i<r-l+1;i++)
    {
        if((r-l+1)%(i)!=0)
            continue;
        int add = 0;
        for(int k=0;;k++)
        {
            if((k+1)*i>r-l+1)
                break;
            for(int j=0;j<i;j++)
            {
                if(S[l+k*i+j]!=S[l+j])
                    break;
                if(j==i-1)
                    add+=1;
            }
        }

        if(add==(r-l+1)/i)
        {
            int point=solve(l,l+i-1)+2+FF(add);
            if(dp[l][r]>point)
            {
                dp[l][r]=point;
                s[l][r]="";
                s[l][r]+=F(add)+'(';
                s[l][r]+=s[l][(1)*i-1+l];
                s[l][r]+=')';
            }
        }
    }
    return dp[l][r];
}
int main()
{
    freopen("folding.in","r",stdin);
    freopen("folding.out","w",stdout);
    scanf("%s",S+1);
    n = strlen(S+1);
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j<=n;j++)
        {
            dp[i][j]=j-i+1;
            for(int k=0;k<j-i+1;k++)
            {
                s[i][j]+=S[i+k];
            }
        }
    }
    solve(1,n);
    cout<<s[1][n]<<endl;
    return 0;
}

 

posted @ 2015-09-04 18:40  qscqesze  阅读(706)  评论(0编辑  收藏  举报