zoukankan      html  css  js  c++  java
  • UVA1630 Folding 区间DP

    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;
    }
  • 相关阅读:
    (ZOJ 3329) One Person Game (概率DP)
    python爬虫之json数据处理
    1034 Head of a Gang 图的遍历,map使用
    1030 Travel Plan Dijkstra+dfs
    vs C++ scanf 不安全
    1021. Deepest Root DFS 求最长无环路径
    1013. Battle Over Cities 用dfs计算联通分量
    无法解析的外部符号
    PAT DFS,BFS,Dijkstra 题号
    1004 Counting Leaves 对于树的存储方式的回顾
  • 原文地址:https://www.cnblogs.com/zxhl/p/5436889.html
Copyright © 2011-2022 走看看