先上题目:

Problem H: Let's call SPaDe a SPaDe

Passing time, walking the passage, as you pass the String Parsing Department(abbreviated SPaDe), you pause, amazed at them by parsing strings way past midnight. At the SPaDe , they are overwhelmed with the stringent requirements to compression recently introduced by the SPaDe's director, Dr. Spade. Any string longer than 4 characters must now be compressed as much as possible, Dr. Spade dictates! "If a string can be expressed shorter, so it must be!", he shouts.

He then yells that abababab can be expressed as just(ab)4 which is only 5 symbols, a whole saving of 3 symbols, and everyone in the Department breaks out in a song of celebration, chanting:

 This is why I'm hot
This is why I'm hot
This is why
This is why
This is why I'm hot
This is why I'm hot
This is why I'm hot
This is why
This is why
This is why I'm hot

but of course given in its compressed form

 (This is why I'm hot)2
(This is why)2
(This is why I'm hot)3
(This is why)3
I'm hot

Given a string S over the alphabet {a,b,c,d} as input, output the length of its most compressed version. The SPaDe has yet to discover nested compression as in ((a)2b)3 so use only one-level compression

Input Format

The first line contains an integer T (1 <= T <= 100), the number of test cases. For each test case there is a line with a string S (5 <= |S| <= 100).

Output Format

For each test case, print on a separate line the minimum length of S after the compression described above.

Sample Input

2
abcda
dabbaabbabadddddccccbbbbbbbbbbbb

Sample Output

5
23

The string from the second example can be compressed into d(abba)2ba(d)5cccc(b)12 .

  题意:给出一个一个串,将其压缩,要求压缩的部分是其循环节,用一对括号括住并且在后面跟上循环次数的数字,不能嵌套压缩,问要所以后的字符串最短有多短(有可能不需要压缩)。

  区间dp+KMP求循环节。

  原始做法dp[i][j]表示第i~j位的字符压缩以后需要最少需要多少长度来保存。然后对于第i~第j位的字符串还需要求一次循环节。这样的时间复杂度是O(n^3)算上100组case的话勉强可以在1s跑完。其实这就是正解了。但是比赛的时候小伙伴觉得可能会TLE,所以优化了一下变成dp[i]=min{dp[k]+(k+1,i)},枚举k。中途WA了一次,原因是get_next写的和正常的有点不一样,所以快要被小伙伴暴打一顿了→_→。

上代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define MAX 102
#define INF (1<<30)
using namespace std; char s[MAX];
int Next[MAX];
int ne[MAX];
int dp[MAX]; void get_next(char* c,int len,int* next){
int k,i;
k=-;i=;
next[]=-;
while(i<=len){
if(k==- || c[i]==c[k]){
k++; i++; next[i]=k;
}else{
k=next[k];
}
}
} inline int getVal(int x){
int ans=;
while(x){
ans++;
x/=;
}
return ans;
} int main(){
//freopen("in_a.txt","r",stdin);
int t,l,m,e,u,v;
scanf("%d",&t);
while(t--){
scanf("%s",s);
int len=strlen(s);
get_next(s,len,Next);
for(int i=;i<len;i++){ dp[i]=i-+;
if((i+)%(i+-Next[i+])==){
m=(i+)/(i+-Next[i+]);
l=+(i+-Next[i+])+getVal(m);
}else l=INF;
dp[i]=min(l,dp[i]); for(int j=;j<i;j++){
get_next(s+j+,i-j,ne);
v=i-j;
if((i-j)%(i-j-ne[i-j])==){
e=(i-j)/(i-j-ne[i-j]);
u=+(i-j-ne[i-j])+getVal(e);
}else u=INF;
v=min(u,v);
dp[i]=min(dp[i],dp[j]+v);
}
}
printf("%d\n",dp[len-]);
}
return ;
}

/*12451*/

UVa - 12451 - Let's call SPaDe a SPaDe的更多相关文章

  1. 棋牌平台开发教程之扎金花大小比较算法在php中的实现

    PHP中扎金花比大小如何实现 在棋牌游戏中,不管是现实的还是线上的,扎金花无疑是最热门棋牌游戏之一,鄙人从小就酷爱扎金花,机缘巧合后面从事了IT行业,话不多说,直接进去正题吧. 扎金花两副牌的比较规则 ...

  2. 品鉴同事发来的炸金花的PHP程序代码

    今天同事发来了一个炸金花的PHP程序,这个代码实现了两个人通过各自的三张牌进行权重计算,得到分数进行比较得到谁的牌大,我觉得里面还有一些问题,代码如下: <?php /** 每张牌的分值为一个2 ...

  3. 详细解读Python中的__init__()方法

    init()方法意义重大的原因有两个.第一个原因是在对象生命周期中初始化是最重要的一步:每个对象必须正确初始化后才能正常工作.第二个原因是init()参数值可以有多种形式. 因为有很多种方式为init ...

  4. Straight Master (贪心)

    题目如下:A straight is a poker hand containing five cards of sequential rank, not necessarily to be the ...

  5. 轻奢当道业绩逆势增长 Kate Spade联手韩国衣恋开拓中国市场_商场报道_中国时尚品牌网

    轻奢当道业绩逆势增长 Kate Spade联手韩国衣恋开拓中国市场_商场报道_中国时尚品牌网 轻奢当道业绩逆势增长 Kate Spade联手韩国衣恋开拓中国市场

  6. 安卓手机的后门控制工具SPADE

    SPADE,一款安卓手机的后门控制工具,安全研究人员可以以此了解和研究安卓后门原理. 首先,我们从网站www.apk4fun.com下载apk文件,如ccleaner.然后,我们安装spade git ...

  7. 序列模式挖掘--SPADE算法

  8. Super Poker II UVA - 12298 FFT_生成函数

    Code: #include<bits/stdc++.h> #define maxn 1000000 #define ll long long #define double long do ...

  9. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

随机推荐

  1. BADI FCODE(菜单) 增强

    菜单增强功能只能用于非依赖于过滤器的一次性BADI(不是多用途的). 目前,菜单增强功能只能与程序增强功能(界面)一起创建. 定义一个没有过滤器的一次性增强 2.Classic Badi在FCODE ...

  2. MySQL性能优化神器Explain

    本文涉及:MySQL性能优化神器Explain的使用 简介 虽然使用Explain不能够马上调优我们的SQL,它也不能给予我们一些调整建议,但是它能够让我们了解MySQL 优化器是如何执行SQL 语句 ...

  3. 无法收集统计信息,怎样优化SQL。

    特殊情况如下 客户的统计信息是固定的,没办法收集统计信息 . SQL profile 是最后考虑方案,因为同样写法sql 比较多,几十条. Parallle 并行客户一般不考虑接受,OLTP 系统. ...

  4. 从 FTP 服务器上下载并保存文件

    本例演示如何运用 C# 中的 FtpWebRequest 等对象从 FTP 服务器上获取文件,并结合 Stream 对象中的方法来保存下载的文件: using System; using System ...

  5. jmeter生成时间的函数

    在一个接口测试中,需要提交的请求中要带时间,在看完jmeter帮忙文档,正好总结一下 1.需求 在一个http请求中请求数据要带有时间,如下: 2.实现 突然想到jmeter自带的函数助手好像是可以实 ...

  6. [转]mysql的查询、子查询及连接查询

    转自:http://www.cnblogs.com/rollenholt/archive/2012/05/15/2502551.html 一.mysql查询的五种子句         where(条件 ...

  7. Spark性能优化指南-高级篇(spark shuffle)

    Spark性能优化指南-高级篇(spark shuffle) 非常好的讲解

  8. eclipse安装提示错误:Failed to load JNI shared library "D:\jdk1.7\client\jvm.dll"

    错误截图如下 原因是jdk32位,eclipse64位导致,修改jdk版本为64位或者修改ecipse版本为32位即可.

  9. IMDB电影排行爬取分析

    一.打开IMDB电影T250排行可以看见250条电影数据,电影名,评分等数据都可以看见 按F12进入开发者模式,找到这些数据对应的HTML网页结构,如下所示 可以看见里面有链接,点击链接可以进入电影详 ...

  10. 编译器:gcc, clang, llvm

    clang Clang是LLVM的前端,可以用来编译C,C++,ObjectiveC等语言.传统的编译器通常分为三个部分,前端(frontEnd),优化器(Optimizer)和后端(backEnd) ...