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;
}
时间: 2024-10-12 11:59:25

UVA1630 Folding 区间DP的相关文章

uva 10003 Cutting Sticks 简单区间dp

// uva 10003 Cutting Sticks 区间dp // 经典的区间dp // dp(i,j)表示切割小木棍i-j所需要的最小花费 // 则状态转移为dp(i,j) = min{dp(i,k) + dp(k,j) + a[j]-a[i]) // 其中k>i && k<j // a[j] - a[i] 为第一刀切割的代价 // a[0] = 0,a[n+1] = L; // dp数组初始化的时候dp[i][i+1]的值为 0,这表示 // 每一段都已经是切割了的,不

黑书例题 Fight Club 区间DP

题目可以在bnuoj.soj等OJ上找到. 题意: 不超过40个人站成一圈,只能和两边的人对战.给出任意两人对战的输赢,对于每一个人,输出是否可能是最后的胜者. 分析: 首先序列扩展成2倍,破环成链. dp[i][j]表示i和j能够相遇对打,那么dp[i][i+n]为真代表可以成为最后胜者. 枚举中间的k,若i和j都能和k相遇,且i和j至少一人能打赢k,那么i和j可以相遇. 复杂度o(n^3) 1 #include<cstdio> 2 #include<cstring> 3 usi

算法复习——区间dp

感觉对区间dp也不好说些什么直接照搬讲义了2333 例题: 1.引水入城(洛谷1514) 这道题先开始看不出来到底和区间dp有什么卵关系···· 首先肯定是bfs暴力判一判可以覆盖到哪些城市····无解直接输出···有解得话就要想想了···· 这道题关键是要发现··如果一个蓄水池所在城市可以覆盖到一些沙漠城市···那么这些沙漠城市肯定是一段····不然假设有一个城市是断开的而两边都被同一个蓄水池流出的水覆盖,这个城市四周的城市都肯定比它矮···(不理解举个反例吧···反正我举不出来)···然后就

合并石子 区间dp水题

合并石子 链接: nyoj 737 描述: 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这两堆石子的和,经过N-1次合并后成为一堆.求出总的代价最小值. tags:最基本的区间dp,这题范围小,如果n大一些,还是要加个平行四边行优化. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring&g

Luogu P2734 游戏 A Game 区间DP

P2734 游戏 A Game 题目背景 有如下一个双人游戏:N(2 <= N <= 100)个正整数的序列放在一个游戏平台上,游戏由玩家1开始,两人轮流从序列的任意一端取一个数,取数后该数字被去掉并累加到本玩家的得分中,当数取尽时,游戏结束.以最终得分多者为胜. 题目描述 编一个执行最优策略的程序,最优策略就是使玩家在与最好的对手对弈时,能得到的在当前情况下最大的可能的总分的策略.你的程序要始终为第二位玩家执行最优策略. 输入输出格式 输入格式: 第一行: 正整数N, 表示序列中正整数的个数

HDU-4283 You Are the One (区间DP)

Problem Description The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot of boys and girls. Now there ar

lightoj1031_区间dp

题目链接:http://lightoj.com/volume_showproblem.php?problem=1031 题目描述: 给出一个数列,两人轮流取数, 取完结束.每次可以取好多个数,但是只能从首或者尾为起点取连续的若干个.问最后两者取数和的绝对值最大为多少? 区间dp: 这道题我是在看了几份阶梯报告之后才想通的,现在想想很符合动态规划的要求 d(i, j)表示取数的人在数组i 到 j中能取的的最大值,然后中间枚举分割点, ans = max(ans, sum[k]-sum[i-1]-d

lightoj1025_区间dp

题目链接:http://lightoj.com/volume_showproblem.php?problem=1025 题目描述: 给出一个字符串,可以任意删除位置的字符,也可以删除任意多个.问能组成多少个回文串? 解题思路: 自从开始学dp,感觉自己智商一直处于离线状态.席八啊啊啊啊啊啊!今天随机到这个题目,看了好久竟然没有看出来是区间DP.知道是区间DP后立马感觉明白. 情景设定 dp[l][r] 表示 区间 [l, r] 内的回文串数目. 状态转移:dp[l][r] = dp[l][r-1

POJ 2955 Brackets (区间DP)

题意:给定一个序列,问你最多有多少个合法的括号. 析:区间DP,dp[i][j] 表示在 第 i 到 第 j 区间内最多有多少个合法的括号. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <ios