Folding
题意:
给定一个串,求能化成的最短循环节串(把重复字符串转化成循环节形式)
分析:
不是太好想,如果让求最短长度还好,dp[i][j],表示区间[i,j]化成的最小长度,dp[i][j]=min(dp[i][k]+dp[k+1][j]),即可但现在要求这个串不知怎么做,想着串能不能跟着转移啊,就用 str[l][r]表示区间[l,r]能化成的最短串,接着就是想怎么跟着转移了,当枚举k时,求出使得最短的k,str[l][r]=str[l][k]+str[k+1][r],再判断是否能再次循环。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
char s[];
string str[][];
int dp[][];
int len(int l,int r){
for(int i=;i<=(r-l+)/;++i){
if((r-l+)%i)continue;
int f=;
for(int j=l;j+i<=r;++j)
{
if(s[j]!=s[j+i]){
f=;
break;
}
}
if(f)return i;
}
return ;
}
int dfs(int l,int r){
if(dp[l][r]!=-)return dp[l][r];
if(l==r){
dp[l][r]=;
str[l][r]=s[l];
return ;
}
int minv=INF,pos;
for(int i=l;i<r;++i){
int tmp=dfs(l,i)+dfs(i+,r);
if(tmp<minv){
pos=i;
minv=tmp;
}
}
str[l][r]=str[l][pos]+str[pos+][r];
int c=len(l,r);
if(c){
int t=(r-l+)/c;
string s1="";
while(t){
s1+=(''+t%);
t/=;
}
reverse(s1.begin(),s1.end());
s1+="("+str[l][l+c-]+")";
if(s1.size()<minv)
{
minv=s1.size();
str[l][r]=s1;
}
}
return dp[l][r]=minv;
}
int main()
{
while(~scanf("%s",s)){
memset(dp,-,sizeof(dp));
int n=strlen(s);
dfs(,n-);
cout<<str[][n-]<<endl;
}
return ;
}
Folding的更多相关文章
- 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 转载注明出处 非常早之前看过有人求助以下这个效果是怎样实现的, 也就是側滑菜单的一个折叠效果,事实上关于这个效果的实现,谷 ...
- 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 ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序
E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...
- Eclipse折叠代码 coffee bytes code folding
提供一个插件下载地址,博客园的: http://files.cnblogs.com/wucg/com.cb.eclipse.folding_1.0.6.jar.zip 将下载的zip文件解压出来的j ...
- poj2176 Folding【区间DP】
Folding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1841 Accepted: 642 Special ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding
地址:http://codeforces.com/contest/765/problem/E 题目: E. Tree Folding time limit per test 2 seconds mem ...
- UVA1630 Folding 区间DP
Folding Description Bill is trying to compactly represent sequences of capital alphabetic characte ...
- Ural 1238 Folding 题解
目录 Ural 1238 Folding 题解 题意 题解 程序 Ural 1238 Folding 题解 题意 定义折叠.展开为: 单个大写英文字母是一个折叠的串,把它展开后是它本身. 如果\(S\ ...
随机推荐
- 在VS2010中使用Git(转)
在之前的一片博客<Windows 下使用Git管理Github项目>中简单介绍了在Windows环境中使用Git管理Github项目,但是是使用命令行来进行操作的,本文将简单介绍下在VS2 ...
- C# 字符串计算表达式
C# 字符串计算表达式 string str="4+4+2.1"; 要的效果: double sum=4+4+2.1: 方案一: 动态计算表达式: 1 public class ...
- Android应用开发学习笔记之菜单
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android中的菜单分为选项菜单(OptionMenu)和上下文菜单(Context Menu).通常使用菜单资源 ...
- git pull使用【转】
转自:http://www.yiibai.com/git/git_pull.html git pull命令的作用是,取回远程主机某个分支的更新,再与本地的指定分支合并.它的完整格式稍稍有点复杂. $ ...
- 利用SOLR搭建企业搜索平台 之——配置文件
运行solr是个很简单的事,如何让solr高效运行你的项目,这个就不容易了.要考虑的因素太多.这里很重要一个就是对solr的配置要了解.懂得配置文件每个配置项的含义,这样操作起来就会如鱼得水! 在so ...
- 巧用ifstream判断文件是否存在
最近在写手写数字的识别软件,训练样例数量巨大而且数字个数不唯一,有可能在中途粘出一部分做测试样例.因此写下面的脚本来获取文件名,之后丢到训练函数里. #include <algorithm> ...
- URAL1057. Amount of Degrees(DP)
1057 简单的数位DP 刚开始全以2进制来算的 后来发现要找最接近x,y值的那个基于b进制的0,1组合 #include <iostream> #include<cstdio&g ...
- poj3683 Priest John's Busiest Day
2-SAT 输出可行解 找可行解的方案就是: 根据第一次建的图建一个反图..然后求逆拓扑排序,建反图的原因是保持冲突的两个事件肯定会被染成不同的颜色 求逆拓扑排序的原因也是为了对图染的色不会发生冲突, ...
- Couchbase 找回登录密码
在项目里,我们使用Couchbase当做缓存层,效果非常好,支持集群与数据监测,可悲的有一次我新建了一个服务器,安装完成Couchbase之后,密码忘了! 怎么也想不起来,Couchbase又没有提供 ...
- 【笨嘴拙舌WINDOWS】tagTEXTMETRIC结构
tagTEXTMETRIC用于定义在window输出文字时字的大小,其结构如下: 我在窗体上写了两句话,来详细解剖该结构(在MM_TEXT模式下输出) tmHeight表示一行文字的高度.改例中值为1 ...