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的更多相关文章

  1. Ural 1238 Folding 题解

    目录 Ural 1238 Folding 题解 题意 题解 程序 Ural 1238 Folding 题解 题意 定义折叠.展开为: 单个大写英文字母是一个折叠的串,把它展开后是它本身. 如果\(S\ ...

  2. HDOJ(1238) KMP

    Substrings http://acm.hdu.edu.cn/showproblem.php?pid=1238 先找到长度最短的字符串,把它的子串和该子串的逆序(按长度从大到小)依次与其他字符串匹 ...

  3. ural1238. Folding(记忆化)

    1238 这算模拟加记忆化吗 找bug找了2个多小时..记忆化部分好想 就是字符串处理部分挫了 一个个复制模拟 各种修改查找 #include <iostream> #include< ...

  4. Codeforces Gym 100002 Problem F "Folding" 区间DP

    Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...

  5. eclipse 插件之Code Folding

    功能: eclipse自带折叠包括方法, import, 注释等得折叠功能, code folding 插件对其增强. 1. 下载插件:( 也可以用link方式, 我的是link安装, jar包网上很 ...

  6. Android Folding View(折叠视图、控件)

    版本号:1.0 日期:2014.4.21 版权:© 2014 kince 转载注明出处 非常早之前看过有人求助以下这个效果是怎样实现的,   也就是側滑菜单的一个折叠效果,事实上关于这个效果的实现,谷 ...

  7. 51NOD 1238 最小公倍数之和 V3 [杜教筛]

    1238 最小公倍数之和 V3 三种做法!!! 见学习笔记,这里只贴代码 #include <iostream> #include <cstdio> #include < ...

  8. 51nod 1238 最小公倍数之和 V3

    51nod 1238 最小公倍数之和 V3 求 \[ \sum_{i=1}^N\sum_{j=1}^N lcm(i,j) \] \(N\leq 10^{10}\) 先按照套路推一波反演的式子: \[ ...

  9. 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 ...

随机推荐

  1. C#.NET 字符串转数组,数组转字符串

    string str = "1,2,3,4,5,6,7";            string[] strArray = str.Split(','); //字符串转数组      ...

  2. android 消息机制

    一.Android应用程序的主线程主要用于更新UI界面,并且主线程不能做耗时操作,否则会引起ANR:这种情况下需要开一个子线程来进行耗时操作,动作完成之后,子线程发消息给主线程通知其更新UI显示,常见 ...

  3. HDU 4770 Lights Against DudelyLights

    Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. CSS3 笔记一(Rounded Corners/Border Images/Backgrounds)

    CSS3 Rounded Corners The border-radius property is a shorthand property for setting the four border- ...

  5. System.IO.File.Create 不会自动释放,一定要Dispose

    这样会导致W3P进程一直占用这个文件 System.IO.File.Create(HttpContext.Current.Server.MapPath(strName)) 最好加上Dispose Sy ...

  6. .sh脚本判断判断某一变量是否为某一数值

    .sh脚本中,判断某一变量(例如:OEM_CUSTOMER_SUPPORT)是否为某一数值(例如:0),并根据条件做不同处理,写法如下: if [ $OEM_CUSTOMER_SUPPORT -eq  ...

  7. EntityFramework Core 学习笔记 —— 包含与排除类型

    原文地址:https://docs.efproject.net/en/latest/modeling/included-types.html 在模型类中包含一种类型意味着 EF 拥有了这种类型的元数据 ...

  8. Codeforces Round #231 (Div. 2) E.Lightbulb for Minister

    题意:有n个点,问在一个m边形内哪个点与这n个点的距离平方和最小 题解:(ai-a0)^2=ai*ai+a0*a0-a*ai*a0 合起来就是a1*a1+...+an*an+n*a0*a0-2*a0* ...

  9. 字典的循环和if语句

    字典是键-值(key-value)存储,循环的时候也是以键为对象 d = {'Michael': 95, 'Tracy': 85,'Bob': 75} for x in d : print x 输出结 ...

  10. Android-Universal-Image-Loader的缓存处理机制

    讲到缓存,平时流水线上的码农一定觉得这是一个高大上的东西.看过网上各种讲缓存原理的文章,总感觉那些文章讲的就是玩具,能用吗?这次我将带你一起看过UIL这个国内外大牛都追捧的图片缓存类库的缓存处理机制. ...