UVA - 1630 Folding(串折叠)(dp---记忆化搜索)

题意:给出一个由大写字母组成的长度为n(1<=n<=100)的串,“折叠”成一个尽量短的串。折叠可以嵌套。多解时可输出任意解。

分析:

1、dp[l][r]为l~r区间可折叠成的最短串的长度。

2、ans[l][r]为l~r区间可折叠成的最短串。

3、先判断当前研究的串是否能折叠,若不能折叠,再枚举分割线,折叠分隔后可折叠的串,以使处理后的串最短。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100 + 10;
const int MAXT = 10000 + 10;
using namespace std;
string s;
string ans[MAXN][MAXN];
int dp[MAXN][MAXN];
int dfs(int l, int r){
    if(dp[l][r] != -1) return dp[l][r];
    int len = r - l + 1;
    if(len == 1){//串的长度为1,不能折叠也不能枚举分割线
        ans[l][r] = s[l];
        return dp[l][r] = 1;
    }
    ans[l][r] = s.substr(l, len);
    int tmp = len;//以下判断串是否能折叠
    for(int i = 1; i <= len / 2; ++i){//枚举循环周期的长度
        if(len % i) continue;
        bool ok = true;
        for(int j = l + i; j <= r; j += i){//判断串是否以周期为i循环
            for(int k = 0; k < i; ++k){
                if(s[l + k] != s[j + k]){
                    ok = false;
                    break;
                }
            }
            if(!ok) break;
        }
        if(ok){//该串可以按周期为i折叠
            char t[10];
            sprintf(t, "%d", len / i);//循环串的长度
            dfs(l, l + i - 1);//循环串自身可能是可折叠的
            string str(t);
            str += "(" + ans[l][l + i - 1] + ")";
            int nowlen = (int)str.size();
            if(nowlen < tmp){//若折叠后的长度小于不折叠,则更新ans[l][r]
                tmp = nowlen;
                ans[l][r] = str;
            }
        }
    }
    if(tmp != len) return dp[l][r] = tmp;//如果可折叠
    for(int i = l; i < r; ++i){//该串不可折叠,枚举分割线
        int x = dfs(l, i);
        int y = dfs(i + 1, r);
        if(x + y < tmp){
            tmp = x + y;
            ans[l][r] = ans[l][i] + ans[i + 1][r];
        }
    }
    return dp[l][r] = tmp;
}
int main(){
    while(cin >> s){
        memset(dp, -1, sizeof dp);
        int len = (int)s.size();
        dfs(0, len - 1);
        printf("%s\n", ans[0][len - 1].c_str());
    }
    return 0;
}

  

时间: 2024-07-30 13:49:52

UVA - 1630 Folding(串折叠)(dp---记忆化搜索)的相关文章

uva 10626 Buying Coke (DP记忆化搜索)

uva 10626 Buying Coke I often buy Coca-Cola from the vending machine at work. Usually I buy several cokes at once, since my working mates also likes coke. A coke in the vending machine costs 8 Swedish crowns, and the machine accept crowns with the va

UVA 10003 Cutting Sticks 区间DP+记忆化搜索

UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的长度L,第二行是切割点的个数n,接下来的n行是切割点在木棍上的坐标. 输出切割木棍的最小费用 前话-区间dp简单入门 区间dp的入门下面博客写的非常好,我就是看的他们博客学会的,入门简单,以后的应用就得靠自己了. https://blog.csdn.net/qq_41661809/article/d

UVa 10651 Pebble Solitaire(DP 记忆化搜索)

Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board with an arrangement of small cavities, initially all but one occupied by a pebble each. The aim of the game is to remove as many pebbles as possible

Uva 10118 Free Candies (DP+记忆化搜索)

The Problem Little Bob is playing a game. He wants to win some candies in it - as many as possible. There are 4 piles, each pile contains N candies. Bob is given a basket which can hold at most 5 candies. Each time, he puts a candy at the top of one

UVA - 10817 Headmaster&#39;s Headache (状压dp+记忆化搜索)

题意:有M个已聘教师,N个候选老师,S个科目,已知每个老师的雇佣费和可教科目,已聘老师必须雇佣,要求每个科目至少两个老师教的情况下,最少的雇佣费用. 分析: 1.为让雇佣费尽可能少,雇佣的老师应教他所能教的所有科目. 2.已聘老师必须选,候选老师可选可不选. 3.dfs(cur, subject1, subject2)---求出在当前已选cur个老师,有一个老师教的科目状态为 subject1,有两个及以上老师教的科目状态为 subject2的情况下,最少的雇佣费用. dp[cur][subje

UVa 10817 (状压DP + 记忆化搜索) Headmaster&#39;s Headache

题意: 一共有s(s ≤ 8)门课程,有m个在职教师,n个求职教师. 每个教师有各自的工资要求,还有他能教授的课程,可以是一门或者多门. 要求在职教师不能辞退,问如何录用应聘者,才能使得每门课只少有两个老师教而且使得总工资最少. 分析: 因为s很小,所以可以用状态压缩. dp(i, s1, s2)表示考虑了前i个人,有一个人教的课程的集合为s1,至少有两个人教的集合为s2. 在递归的过程中,还有个参数s0,表示还没有人教的科目的集合. 其中m0, m1, s0, s1, s2的计算用到位运算,还

状压DP+记忆化搜索 UVA 1252 Twenty Questions

题目传送门 1 /* 2 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 3 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 4 若和答案(自己拟定)相差小于等于1时,证说明已经能区分了,回溯.否则还要加问题再询问 5 */ 6 /************************************************ 7 * Author :Running_Time 8 * Created Time :

11782 - Optimal Cut(树形DP+记忆化搜索)

题目链接:11782 - Optimal Cut 题意:按前序遍历给定一棵满二叉树,现在有k次,可以选k个节点,获得他们的权值,有两个条件: 1.一个节点被选了,他的子节点就不能选了. 2.最终选完后,根到所有叶子的路径上,都要有一个被选的节点. 思路:树形dp,dp[u][k]代表在结点u,可以选k个节点,那么就分两种情况 选u节点,dp[u][k] = node[u]; 选子节点之和,那么就把k次分配给左右孩子,dp[u][k] = max(dp[u][k], dp[u][i], dp[u]

[hihocoder 1033]交错和 数位dp/记忆化搜索

#1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0,?a1,?...,?an?-?1,定义交错和函数: f(x)?=?a0?-?a1?+?a2?-?...?+?(?-?1)n?-?1an?-?1 例如: f(3214567)?=?3?-?2?+?1?-?4?+?5?-?6?+?7?=?4 给定 输入 输入数据仅一行包含三个整数,l,?r,?k(0?≤?l?≤?r?≤?1018,?|k|