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 ...
随机推荐
- sql server 中一次insert 多条的写法
1.SELECT INTO FROM语句 注意此处 要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中.示例如下 2.INSERT ...
- 谷歌大神Jeff Dean:大规模深度学习最新进展 zz
http://www.tuicool.com/articles/MBBbeeQ 在AlphaGo与李世石比赛期间,谷歌天才工程师Jeff Dean在Google Campus汉城校区做了一次关于智能计 ...
- linux中非root用户使用wireshark进行抓包
开始的时候我是在终端中使用sudo 命令打开 wireshark 的,因为如果不这样的话 wireshark 就没法抓包啊.偶尔抓一次包就使用这样的方式提权. 今天使用 wireshark 的时候特意 ...
- 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数008,matrix,矩阵函数
** 3dFromFundamentalMatrix( Rows1, Cols1, Rows2, Cols2, CovRR1, CovRC1, CovCC1, CovRR2, CovRC2, CovC ...
- 关于华擎X99+5820K
受到之前Intel I7-5775C的困扰,于是直接整套平台换掉. 把Z97+I7-5775C+DDR3换成了X99+5820K+DDR4. 但是依然不理想,又是另外一个坑. 组装好后安装系统的过程 ...
- ListView的LayoutParams设置
// Temp is the root view that was found in the xml final View temp = createViewFromTag(root, name, a ...
- oracle表的操作简述
单表的操作!(代码完全可以用手工代替,写下来为了记忆)1.建立表create table HKB_TABLE_MODIFY( NAME VARCHAR2(6), AGE VARCHAR2(3), ...
- 最快速的Android开发环境搭建ADT-Bundle及Hello World
ADT-Bundle for Windows 是由Google Android官方提供的集成式IDE,已经包含了Eclipse,你无需再去下载Eclipse,并且里面已集成了插件,它解决了大部分新手通 ...
- win api 实现 AES加密、解密
WCHAR szPasswod[] = {L"goodpasswod" }; HCRYPTHASH hHash; HCRYPTPROV hCryptProv; HCRYPTKEY ...
- 使用Android studio 出现的问题解决
问题一.安装过程中出现的报错Failed to install Intel HAXM 解决:重启电脑,启动BIOS,我的是惠普的,开机时按F10就进去了 切换到Configuration选项,将设置I ...