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\ ...
随机推荐
- Keil MDK编译器(V4.03)与J-LINK使用
前几天进手了一个J-LINK,因为H-JTAG毕竟对MDK支持的不是太完美,比如用keil mdk上面的下载按钮烧录程序,不是直接就能烧录进去,而是弹出H-Flash软件,再手工选择文件烧录:而且用H ...
- android中使用html作布局文件
在android开发中,通常使用xml格式来描述布局文件.就目前而言,熟悉android布局及美化的人员少之又少,出现了严重的断层.大部分企业,其实还是程序员自己动手布局.这样既浪费时间和精力,也未必 ...
- iOS iphone屏幕分析(岂止而大)
在写本文前,我必须介绍几点内容:第一点:屏幕上面显示的内容多少和屏幕的尺寸大小无关第二点:屏幕上面显示的内容多少和分辨率完全无关第三点:屏幕上面显示的内容多少和屏幕尺寸.屏幕分辨率.PPI等都是无关的 ...
- iOS CoreMotion框架(传感器)
我们知道iOS的应用真的太多了,很多应用让我们惊叹不已!!!很多意想不到的应用! 比如: 1.电子罗盘指南针之类的应用-让我们知道方向. 2.运动类型软件-让我们知道我们跑步多少公里. 3.社交软件中 ...
- C++:构造函数默认的参数声明
C++函数的默认参数指的是在函数声明或者定义时给形式参数指定默认值,从而在调用参数时可以少写参数,少掉的参数用默认值代替.LZ的Display()函数的代码看起来似乎是可以有s2和s3两个默认参数,那 ...
- Linux系统管道命令符
管道命令符“|”的作用是将前一个命令的标准输出作为后一个命令的标准输入,格式为“命令A | 命令B” 以下实例中,通过grep命令搜索关键字“/sbin/nologin”在/etc/passwd中查找 ...
- 14_把文件存放在SDCard
权限添加 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> /** ...
- dojo 四 类的构造函数和父方法的调用
与java类一样,在Dojo里也可以定义constructor 构造函数,在创建一个实例时可以对需要的属性进行初始化.//定义一个类Mqsy_yj var Mqsy_YJ = declare(null ...
- DbProviderFactories.GetFactoryClasses
using System.Data.Common; private void Method1() { DataTable table = DbProviderFactories.GetFactoryC ...
- 《OD学hadoop》第一周0625
一.实用网站 1. linux内核版本 www.kernel.org 2. 查看网站服务器使用的系统 www.netcraft.com 二.推荐书籍 1. <Hadoop权威指南> 1- ...