码迷,mamicode.com
首页 > 其他好文 > 详细

UVA1630 Folding 区间DP

时间:2016-04-27 00:11:00      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

Folding

 

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

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 intput

AAAAAAAAAABABABCCD
NEERCYESYESYESNEERCYESYESYES

  sample output

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

题意:
  
  给你一串字符串,让你简化

题解:
  
  区间dp
  有几个需要注意的点,就是简化后加上个数和两个()可能会比原来的子串还要长。

#include<bits/stdc++.h>
using namespace std;
const int INF= 0x3f3f3f3f;
string str;
int DP[110][110];
string fold[110][110];
int judge(int l,int r){
    for(int i=1;i<=(r-l+1)/2;i++)
    {
        if((r-l+1)%i) continue;
        bool flag=true;
        for(int j=l;j+i<=r;j++)
        {
            if(str[j]!=str[j+i])
            {
                flag=false;
                break;
            }
        }
        if(flag) return i;
    }
    return false;
}
int fun(int l,int r){
    if(DP[l][r]!=-1) return DP[l][r];
    if(l==r){
        DP[l][r]=1;
        fold[l][r]=str[l];
        return 1;
    }
    int k;
    int re=INF;
    for(int i=l;i<r;i++)
    {
        int tmp=fun(l,i)+fun(i+1,r);
        if(tmp < re) { k=i; re=tmp; }
    }
    fold[l][r]=fold[l][k]+fold[k+1][r];
    int len=judge(l,r);
    if(len){
        char t[10];
        sprintf(t,"%d",(r-l+1)/len); //对于一个超过十的整数快速将他转化为字符串形式
        string newstr=t+string("(")+fold[l][l+len-1]+string(")");
        if(newstr.size()<re){
            re=newstr.size();
            fold[l][r]=newstr;
        }
    }
    DP[l][r]=re;
    return re;
}
int main() {
    while(cin>>str){
        int R=str.size()-1;
        memset(DP,-1,sizeof(DP));
        fun(0,R);
        cout<<fold[0][R]<<endl;
    }
    return 0;
}

 

 

UVA1630 Folding 区间DP

标签:

原文地址:http://www.cnblogs.com/zxhl/p/5436889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!