Problem Description

When you arrived at Charles de Gaulle airport, you naively accepted a ride to Paris by an unauthorized driver who offered you “competitive prices”. The end result was a disaster: not only was the price extremely high, but the driver made the trip much longer than necessary in order to justify that price.

You noticed the scam because you were traveling several times by the same place: indeed, you have such a good memory that you can remember very well the path you followed, including each of the loops that your scammer forced you to take.

Now, you are at the police station to fill a complaint against this driver and an officer asks you to tell your story. She even asks you to give all the details of the path you took. Because you do not want to lose yet another couple of hours in doing so, you decide to give a compressed version of it.

Suppose you remember you traveled through places A, B, C, D, A, B, C, D. In this case, you prefer telling the officer “I followed twice the path ABCD”, rather than “I followed the path ABCDABCD”. Given that your path repeated the same sequence of places, this will significantly shorten your statement, without missing any detail.

More precisely, you have to write a program that takes as input the list of places you traveled through, and which returns the size of the shortest compressed form of this path. Such a compressed path can either be:

  • a single place through which you traveled, called an “atomic path”;

  • the concatenation of two compressed paths;

  • the repetition of a compressed path, i.e., (C)n, meaning that you traveled through the path described by C, n times in a row.

The size of a compressed path is defined as the number of atomic paths it contains.

Input

5 test cases and for each case:

The input consists of two lines:
  • The first line contains one integer, N, the length of the path.

  • The second line contains the path, described as a string of size N. Each location is described by an alphanumeric character: either a digit (from ‘0’ to ‘9’), a lowercase letter (from ‘a’ to ‘z’) or an uppercase letter (from ‘A’ to ‘Z’).

Output

For each test case:

The output should consist of a single line, whose content is an integer, the size of the shortest compressed path.

Sample Explanation 1

The shortest compressed form of the path is (((a)3b)2(c)2d)2. The atomic paths it contains are a, b, c and d. Hence, it has size 4.

Sample Explanation 2

The shortest compressed form of the path is (a)2ba. The atomic paths it contains are a, b, and a. Hence, it has size 3.

Sample Input

22
aaabaaabccdaaabaaabccd
4
aaba

Sample Output

4
3

题目大意

给出你一个长度为n的字符串,将字符串中重复的连续字符子串压缩,例如将ababab 压缩成(ab)3,问最少可以用多少个字符来表示字符串(数字省略)

题解

首先分析一下题目,如果一个字符子串(长度为len)可以被压缩,那么肯定存在一个该字符串的子串(长度为k)使得k*x=len,即len%k==0,

如果一个字符串不可以被压缩,那么肯定不存在上述子串,那么我们可以将该字符串分成两个子串,而该字符串最少能被压缩的长度就是两个子串的压缩长度的和

由此我们根据上述两点可以得到动态规划的递推公式

f[i][j]表示区间i-j的被压缩后最少压缩成几个字符

初始状态显然是f[i][i]=1;

由第一个推断可以知道转移状态为f[i][j]=min{f[i][j],f[i][i+len-1]} (j-i+1)%len==0 && 子串中i到i+len-1等于i+len到i+2*len-1等于i+2*len到i+3*len-1 等等

由第二个推断可以知道 转移状态为f[i][j]=min{f[i][j],f[i][k]+f[k+1][j]} i<=k<j

答案保存在f[1][n]中

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int f[710][710];
 4 int len;
 5 char s[1000];
 6 bool check(int l1,int l2,int len){
 7     for (int i=1;i<=len;i++)
 8         if (s[l1+i-1]!=s[l2+i-1]) return 0;
 9     return 1;
10 }
11 bool check1(int l,int len,int k){
12     for(int i=1;i<k;i++)
13         if (!check(l,l+i*len,len)) return 0;
14     return 1;
15 }
16 int main(){
17     while (cin>>len){
18         cin>>s+1;
19         for (int i=0;i<710;i++)
20             for (int j=0;j<710;j++)
21                 f[i][j]=2147483647;
22         for (int i=1;i<=len;i++)
23             f[i][i]=1;
24         for (int i=2;i<=len;i++){
25             for (int j=1;j<=len && j+i-1<=len;j++){
26                 for (int xxx=1;xxx<i;xxx++)
27                     if (i%xxx==0){
28                         if (check1(j,xxx,i/xxx)){
29                             f[j][j+i-1]=min(f[j][j+xxx-1],f[j][j+i-1]);
30                         }
31                     }
32                 for(int k=j;k<j+i-1;k++)
33                     f[j][j+i-1]=min(f[j][j+i-1],f[j][k]+f[k+1][j+i-1]);
34             }
35         }
36         cout<<f[1][len]<<endl;
37     }
38 }

转载于:https://www.cnblogs.com/beafreeman/p/11005825.html

东北大学5月校赛c题相关推荐

  1. 武理校赛A题 ljw的剥削(思维 + map应用)

    武理校赛A题 ljw的剥削(思维 + map应用) 牛客链接 题意: 给定 a[],b[] 两个长度同为 n 的数组,经过一系列操作后, 使 p = ∑ i = 1 n m a x ( ( a i − ...

  2. 题解---2015年浙江理工12月校赛

    A: 孙壕请一盘青岛大虾呗 A题比较容易,因为范围只有10,所以暴力搜索一下就好了,简单递归题 1 #include<cstdio> 2 #include<cstring> 3 ...

  3. 2022年第十二届APMCM亚太杯1月增赛E题思路分享

    2022年亚洲及太平洋地区建模数学竞赛问题E 有多少颗核弹可以摧毁地球? 题目回顾: 1.基本数据分析 a) 哪些国家曾经拥有过核武器? b) 在过去的20年中,哪个国家的核武器库存减少或增加得最多? ...

  4. SDUT 校赛 D题 魔戒(bfs+四维数组)

    魔戒 Time Limit: 1000MS  Memory Limit: 65536KB Problem Description 蓝色空间号和万有引力号进入了四维水洼,发现了四维物体--魔戒. 这里我 ...

  5. QAU 18校赛 J题 天平(01背包 判断能否装满)

    问题 J: 天平 时间限制: 1 Sec  内存限制: 128 MB 提交: 36  解决: 9 [提交][状态][讨论版][命题人:admin] 题目描述 天平的右端放着一件重量为w的物品.现在有n ...

  6. 埃森哲杯第十六届上海大学程序设计联赛春季赛暨上海高校金马五校赛E题小Y吃苹果

    链接:https://www.nowcoder.com/acm/contest/91/E 题意: 小Y买了很多苹果,但他很贪吃,过了几天一下就吃剩一只了.每一天小Y会数出自己的苹果个数X,如果X是偶数 ...

  7. 埃森哲杯第十六届上海大学程序设计联赛春季赛暨上海高校金马五校赛H题小Y与多米诺骨牌(线段树优化dp)

    题意 题目链接:https://www.nowcoder.com/acm/contest/91/H 来源:牛客网 题解 设l[i]l[i]l[i]为向左推第iii个骨牌最远能影响到的骨牌的编号,r[i ...

  8. 埃森哲杯第十六届上海大学程序设计联赛春季赛暨上海高校金马五校赛A题Wasserstein Distance

    题目链接:https://www.nowcoder.com/acm/contest/91/A 题意:给两组数据,求把A变成B状态需花费的最小体力. 思路:s=s+abs(a[i]-b[i]);a[i+ ...

  9. 十九届 浙大 校赛 A题 Thanks, TuSimple!

    题目链接: http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5969 Thanks, TuSimple! Time ...

最新文章

  1. 如何格式化电脑_U盘提示格式化后如何恢复数据
  2. python新手程序员工资-程序员吐槽新同事:连我实习水平都不到,工资是我的1.7倍...
  3. 按键处理技巧(状态机)
  4. ThreadPoolExecutor线程池,shutdown和shutdownNow关闭线程池方式对比,以及确保线程池能够彻底关闭的一种方式
  5. Python 数据分析三剑客之 NumPy(六):矩阵 / 线性代数库与 IO 操作
  6. Percona XtraBackup User Manual 阅读笔记
  7. DB2常用错误代码大全
  8. 微软发布的新开源编程语言 Power Fx
  9. Configure universal group membership caching
  10. mysql gitd 数据结构同步失败_mysql 5.7 gtid主从同步错误修复
  11. 关于将网易有道词典单词本导出到必应词典生词本的尝试
  12. 盘点wps函数公式大全
  13. 计算机犯罪率增长,我国的计算机年犯罪率的增长是()
  14. 小米路由器3 mysql_小米路由器3 opkg安装
  15. 天线越大越好吗_路由器的天线是不是越多越好?告知你真实答案,很多人都买错了...
  16. cilium系列之四:使用阿里云vpc作为ipam
  17. Unity3D打印拓展XMDebug
  18. vue3中Provide/Inject的使用
  19. Mysql 和 Oracle 的基本知识总结
  20. 智能座舱域控制器功能自动化测试方案

热门文章

  1. 团队项目简介-电梯演讲
  2. 脑洞全开YY无罪-益智类游戏“脑力大战”引发思考
  3. 蓝桥杯 — 星系炸弹( 在X星系的广袤空间中漂浮着许多X星人造“炸弹”)
  4. 100 个知名网站源码
  5. Python学习笔记(十九)——Matplotlib入门
  6. 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。...
  7. python图像数据分析,【笔记】python数据分析——应用案例之图像负片
  8. 《论文写作》思考与总结
  9. 1439. airship
  10. SQLServer2017在使用insert在表中插入数据,显示列名无效