UVa - 12451 - Let's call SPaDe a SPaDe

先上题目:

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写的和正常的有点不一样,所以快要被小伙伴暴打一顿了→_→。

上代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cmath>
 5 #define MAX 102
 6 #define INF (1<<30)
 7 using namespace std;
 8
 9 char s[MAX];
10 int Next[MAX];
11 int ne[MAX];
12 int dp[MAX];
13
14 void get_next(char* c,int len,int* next){
15     int k,i;
16     k=-1;i=0;
17     next[0]=-1;
18     while(i<=len){
19         if(k==-1 || c[i]==c[k]){
20             k++;    i++;    next[i]=k;
21         }else{
22             k=next[k];
23         }
24     }
25 }
26
27 inline int getVal(int x){
28     int ans=0;
29     while(x){
30         ans++;
31         x/=10;
32     }
33     return ans;
34 }
35
36 int main(){
37     //freopen("in_a.txt","r",stdin);
38     int t,l,m,e,u,v;
39     scanf("%d",&t);
40     while(t--){
41        scanf("%s",s);
42        int len=strlen(s);
43        get_next(s,len,Next);
44        for(int i=0;i<len;i++){
45
46            dp[i]=i-0+1;
47            if((i+1)%(i+1-Next[i+1])==0){
48                m=(i+1)/(i+1-Next[i+1]);
49                l=2+(i+1-Next[i+1])+getVal(m);
50            }else l=INF;
51            dp[i]=min(l,dp[i]);
52
53            for(int j=0;j<i;j++){
54                get_next(s+j+1,i-j,ne);
55                v=i-j;
56                if((i-j)%(i-j-ne[i-j])==0){
57                     e=(i-j)/(i-j-ne[i-j]);
58                     u=2+(i-j-ne[i-j])+getVal(e);
59                }else u=INF;
60                v=min(u,v);
61                dp[i]=min(dp[i],dp[j]+v);
62            }
63        }
64        printf("%d\n",dp[len-1]);
65     }
66     return 0;
67 }

/*12451*/

UVa - 12451 - Let's call SPaDe a SPaDe

时间: 2024-11-01 08:25:47

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

词组习语3057组

superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 1 Anointing of the Sick British English 2 Civvy Street Clerk of the Closet 3 I mean I must say 4 I suppose so I will thank you to do something 5 Incoming mail server Lithium battery 6 M

纯CSS实现扑克牌效果

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="con

296句原味英语

1. I wasn't born yesterday.(我又不是三岁小孩) 2. How do I address you?(我怎么称呼你) 3. She turns me off.(她使我厌烦.) 4. So far so good.(目前为止,一切都好.) 5. Be my guest.(请便.别客气) 6. That was a close call.(太危险了/千钧一发) 7. Far from it.(一点也不.) 8. It's a pain in the neck[麻烦的事(人)]

英语文章、常用短语部分摘选集锦

**=================文章====================** Touchy Topics 敏感话题 In North America when people meet each other for the first time, they talk about things like family, work, school or sports. They ask questions like "Do you have any brothers or sisters?&

纯CSS实现扑克牌效果,太牛了吧

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <meta http-equiv="

棋牌平台制作教程之php中的炸金花大小比较算法

PHP中扎金花比大小如何实现 在棋牌游戏中,不管是现实的还是线上的,炸金花无疑是最热门棋牌游戏之一,鄙人从小就酷爱炸金花,机缘巧合后面从事了IT行业,话不多说,直接进去正题吧. 炸金花两副牌的比较规则就不说了,注明一下是顺子的时候 : JQK < A23 < QKA 思路:炸金花下面是个人一点建议,供大家参考!(有哪里不对的欢迎指教 棋牌平台定制: www.yasewl.com) 1. 随机生成两幅牌,每副牌结构为 复制代码 代码如下:array(      array('Spade','K'

C++ ShowHand

[内容] 模拟一个梭哈(Show hand) 牌戏,可以洗牌(shuffle).发牌(Deal).算牌型(GetType). 程序为控制台类型,输入Y发牌,输入N退出,不区大小写.输入Y后,先洗牌,把洗过的57张牌都打印出来,再随机抽 5 张,显示它们的牌型和大小. 打印时,牌的表示方法如下: 数字在前,花色在后. 2到9,用对应的数字表示:10 用 T 表示:JQKA就用对应的字母表示 花色:Club(草花) < Diamond(方块) < Heart(红桃) < Spade (黑桃)

Straight Master (贪心)

题目如下:A straight is a poker hand containing five cards of sequential rank, not necessarily to be the same suit. For example, a hand containing 7 club, 6 spade, 5 spade, 4 heart and 3 diamond forms a straight. In this problem, we extend the definition

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

PHP中扎金花比大小如何实现 在棋牌游戏中,不管是现实的还是线上的,扎金花无疑是最热门棋牌游戏之一,鄙人从小就酷爱扎金花,机缘巧合后面从事了IT行业,话不多说,直接进去正题吧. 扎金花两副牌的比较规则就不说了,注明一下是顺子的时候 : JQK < A23 < QKA 下面是个人一点建议,供大家参考!(有哪里不对的欢迎指教 棋牌平台开发: www.yasewl.com) 思路:扎金花 1. 随机生成两幅牌,每副牌结构为 复制代码 代码如下:array( array('Spade','K'), a