1238. Folding
http://acm.timus.ru/problem.aspx?space=1&num=1238
DP+记忆化搜索
思路不难,关键是最优结果的储存问题,为了编写方便,直接用string储存最优结果
虽然速度慢了一些,不过写起来方便
代码:
#include<iostream>
#include<stack>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<cmath> using namespace std; typedef long long ll;
typedef pair<int,int> pp;
const int INF=0x3f3f3f3f;
const int N=103;
string d[N][N];
string s;
char c[]={'0','1','2','3','4','5','6','7','8','9'};
string trans(int k)
{
string stmp="";
while(k)
{
stmp.insert(stmp.begin(),c[k%10]);
k=k/10;
}
return stmp;
}
bool ok(int l,int r,int x)
{ for(int i=l+x;i<=r;i++)
if(s[i]!=s[i-x])
return false;
return true;
}
string dp(int l,int r)
{
if(d[l][r]!="")
return d[l][r];
if(l==r)
{
d[l][r].push_back(s[l]);
return d[l][r];
}
for(int i=l;i<=r;++i)
d[l][r].push_back(s[i]);
string stmp="";
string st,sx;
int n=r-l+1;
for(int i=1;i<=n/2;++i)
if(n%i==0&&ok(l,r,i))
{
int k=n/i;
st=trans(k);
sx=dp(l,l+i-1); if(st.size()+sx.size()+2<d[l][r].size())
d[l][r]=st+"("+sx+")";
}
for(int i=l;i<r;++i)
if(dp(l,i).size()+dp(i+1,r).size()<d[l][r].size())
d[l][r]=dp(l,i)+dp(i+1,r);
return d[l][r];
}
int main()
{
//freopen("data.in","r",stdin);
while(cin>>s)
{
for(int i=0;i<N;++i)
for(int j=0;j<N;++j)
d[i][j]="";
cout<<dp(0,s.size()-1)<<endl;
}
return 0;
}
1238. Folding的更多相关文章
- Ural 1238 Folding 题解
目录 Ural 1238 Folding 题解 题意 题解 程序 Ural 1238 Folding 题解 题意 定义折叠.展开为: 单个大写英文字母是一个折叠的串,把它展开后是它本身. 如果\(S\ ...
- HDOJ(1238) KMP
Substrings http://acm.hdu.edu.cn/showproblem.php?pid=1238 先找到长度最短的字符串,把它的子串和该子串的逆序(按长度从大到小)依次与其他字符串匹 ...
- ural1238. Folding(记忆化)
1238 这算模拟加记忆化吗 找bug找了2个多小时..记忆化部分好想 就是字符串处理部分挫了 一个个复制模拟 各种修改查找 #include <iostream> #include< ...
- Codeforces Gym 100002 Problem F "Folding" 区间DP
Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...
- eclipse 插件之Code Folding
功能: eclipse自带折叠包括方法, import, 注释等得折叠功能, code folding 插件对其增强. 1. 下载插件:( 也可以用link方式, 我的是link安装, jar包网上很 ...
- Android Folding View(折叠视图、控件)
版本号:1.0 日期:2014.4.21 版权:© 2014 kince 转载注明出处 非常早之前看过有人求助以下这个效果是怎样实现的, 也就是側滑菜单的一个折叠效果,事实上关于这个效果的实现,谷 ...
- 51NOD 1238 最小公倍数之和 V3 [杜教筛]
1238 最小公倍数之和 V3 三种做法!!! 见学习笔记,这里只贴代码 #include <iostream> #include <cstdio> #include < ...
- 51nod 1238 最小公倍数之和 V3
51nod 1238 最小公倍数之和 V3 求 \[ \sum_{i=1}^N\sum_{j=1}^N lcm(i,j) \] \(N\leq 10^{10}\) 先按照套路推一波反演的式子: \[ ...
- Chrome Dev Tools: Code Folding in CSS and Javascript for improved code readiability
Note : Apply for google chrome canary. You can fold code blocks in CSS (and Sass) and javascript fil ...
随机推荐
- 11.写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名
//首先列出需要被操作的url $url_path = "http://www.sina.com.cn/abc/de/fg.php?id=1"; 方法一: ...
- 4.用PHP打印出前一天的时间格式是2006-5-10 22:21:21
echo date('Y-m-d H:i:s', strtotime('-1 days'));
- C#实现执行多条SQl语句,实现数据库事务
C#实现执行多条SQl语句,实现数据库事务 在数据库中使用事务的好处,相信大家都有听过银行存款的交易作为事务的一个例子.事务处理可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的 ...
- 几个精彩的DMV
--统计表的增删改次数,反映表的使用程度 SELECT DB_NAME([database_id]) AS [Database] ,iops.[object_id] AS [ObjectID] ,QU ...
- Git Permission denied (publickey).
有可能, jenkins slave service or jenkins service的logon 账户没有设置好
- IOS手势UIGestureRecognizer
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有6个子类处理具体的手势: 1.UITapGestureRecognizer (任意手指任意次数的点击) // 点击次数 ...
- bootstrap 下拉菜单不显示的解决办法
bootstrap 下拉菜单不显示,最后改成如下正常显示: <div class="btn-group open"> <ul class="dropdo ...
- C# 的Brush 及相关颜色的操作 (并不是全转)
C# 的Brush 及相关颜色的操作 2013-12-13 14:08 4977人阅读 评论(0) 收藏 ...
- python发送邮件方法
1.普通文本邮件 #!/usr/bin/env python # -*- coding:utf-8 -*- import smtplib from email.mime.text import MIM ...
- 一步一步教你编写与搭建自动化测试框架——python篇
[本文出自天外归云的博客园] 这两天用python写了一个自动化测试框架,取名为Auty.准备用来做Web方面的接口测试,以下为Auty框架一步一步的搭建过程——