首页 技术 正文
技术 2022年11月19日
0 收藏 838 点赞 2,857 浏览 2504 个字

先上题目:

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.

UVa – 12451 – Let's call SPaDe a SPaDe

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*/

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918