A common way to uniquely encode a string is by replacing its consecutive repeating characters (or “chunks”) by the number of times the character occurs followed by the character itself. For example, the string “aabbbaabaaaa” may be encoded as “2a3b2a1b4a”. (Note for this problem even a single character “b” is replaced by “1b”.)

Suppose we have a string S and a number k such that k divides the length of S. Let S1 be the substring of S from 1 to k, S2 be the substring of S from k + 1 to 2k, and so on. We wish to rearrange the characters of each block Si independently so that the concatenation of those permutations S’ has as few chunks of the same character as possible. Output the fewest number of chunks.

For example, let S be “uuvuwwuv” and k be 4. Then S1 is “uuvu” and has three chunks, but may be rearranged to “uuuv” which has two chunks. Similarly, S2 may be rearranged to “vuww”. Then S’, or S1S2, is “uuuvvuww” which is 4 chunks, indeed the minimum number of chunks.

Program Input

The input begins with a line containing t (1 ≤ t ≤ 100), the number of test cases. The following t lines contain an integer k and a string S made of no more than 1000 lowercase English alphabet letters. It is guaranteed that k will divide the length of S.

Program Output

For each test case, output a single line containing the minimum number of chunks after we rearrange S as described above.

INPUT

2
5 helloworld
7 thefewestflops

OUTPUT

8
10

这是我自己写的,我并没有用动态规划,而是用了分类讨论,由于从左到右的过程中,每两个相邻块只有三种情况:

①没有相同字符;

②有一个相同字符;

③有两个以上相同字符;

第一种情况是很简单,第二中情况也同样简单,只不过要标记该字符,第三种情况的话不用标记,也很简单。

在标记了之后其实要进行更多情况的分类。

由于分类讨论不够严谨,WA了很多次。

int dp(int m)

{
int ans = flag[0][0];

for(int i = 1; i < m; i++)
{
int cnt1 = 0, cnt2 = 0;
int which1 = -1, which2 = -1;

for(int j = 1; j < 27; j++)
{
if(flag[i][j] == 1)
{
if(flag[i-1][j] == 1)
{
cnt1++;
which1 = j;
}
else if(flag[i-1][j] == -1)
{
cnt2++;
which2 = j;
}
}
}

if(cnt1 == 0)
{
if(cnt2 == 1 && flag[i-1][0] == 1)
{
ans += flag[i][0] - 1;
flag[i][which2] = -1;
}
else ans += flag[i][0];
}
else if(cnt1 == 1)
{
if(cnt2 == 1 && flag[i-1][0] == 1)
{
ans += flag[i][0] - 1;
}
else
{
flag[i][which1] = -1;
ans += flag[i][0] - 1;
}
}
else //cnt >= 2
{
ans += flag[i][0] - 1;
}
}

return ans;

}

我还是在网上找了动态规划的方案:

int dp(int k, int m)
{
for(int x = 0; x < 1000; x++)
{
for(int y = 0; y < 27; y++) d[x][y] = INF;
}

for(int i = 1; i <= 26; i++)
{
if(data[0][i]) d[0][i] = data[0][0];
}

int chunck = data[0][0];

for(int t = 1; t < m; t++)
{
int best = INF;

for(int i = 1; i <= 26; i++) if(data[t][i]) //当前块以字符('a'+i)开头
{
for(int j = 1; j <= 26; j++) if(data[t][j] && (i != j || data[t][0] == 1))
//当前块以字符('a'+j-1)结尾,有限制,即开头不等于结尾除非该块只有一种字符
{
//如果前面的块有以当前块开头的字符结尾的排列...
if(d[t-1][i] < INF)
{
d[t][j] = min(d[t][j], d[t-1][i] + data[t][0] - 1);
}
else //否则...
{
d[t][j] = min(d[t][j], chunck + data[t][0]);
}
if(best > d[t][j]) best = d[t][j];
}
}

chunck = best;
}

int ans = d[m-1][1];
for(int j = 2; j <= 26; j++)
{
if(d[m-1][j] < ans)
{
ans = d[m-1][j];
}
}
return ans;
}

这里结合了我自己的处理方式进行了一些改进,这是一道多维的动态规划题目,其实我一开始也在找最优子结构,可是一直没有想到理想的状态转移方程。

这里用到的是:

d[i][q] = d[i-1][p] 如果两个块都有字符相同字符p(当然q有限制条件q!=p除非第i个块只有这一种字符)

d[i][q] = best[i-1]+n[i]

评价:

①对多维的动态规划部熟练,连最优子结构都发现不了。

Fewest Flops相关推荐

  1. Uva 11552 Fewest Flops

    Fewest Flops A common way to uniquely encode a string is by replacing its consecutive repeating char ...

  2. UVA 11552——Fewest Flops

    题意:给定一个长为s的字符串(其中s为k的倍数),然后按照前后分成s/k 组,每组之内可以重排,使得重排后的块数最少(一段连续的字母算是一块). 思路:区间dp,dp[i][fa]表示前i组且第i+1 ...

  3. C - Fewest Flops

    这个题的意思是: "aabbbaabaaaa" may be encoded as "2a3b2a1b4a" 给出一个字符串,将其按照每组k个字符切开,将每组的 ...

  4. 《算法竞赛入门经典——训练指南》第一章相关内容

    #<算法竞赛入门经典--训练指南>第一章相关内容 希望各位大牛能指导! 红色为已经做了的...黄色背景是还有不懂地方,希望在年前能刷完第一章啊.... 更新版.google上貌似又加了ex ...

  5. UVa在线比赛单题汇总-----DP专题

    动态规划基础 例题 LA 3882 UVa 3882 - And Then There Was One 递推------------无力orz UVa 10635 10635 - Prince and ...

  6. 神经网络中参数量parameters和FLOPs计算

    一.神经网络中参数量parameters和FLOPs计算 CNN中的parameters分为两种:W和b,对于某一个卷积层,它的parameters的个数为: (Kh∗Kw∗Cin)∗Cout+Cou ...

  7. 超越GhostNet!吊打MobileNetV3!MicroNet通过极低FLOPs实现图像识别(文末获取论文)

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 本文提出Micro-Factorized卷积,将点和深度卷积分解为低秩矩阵,并提出新的激活函数,称为D ...

  8. CNN模型复杂度(FLOPs、MAC)、参数量与运行速度

    CNN模型复杂度(FLOPs.MAC).参数量与运行速度 先转载一下,有空再来整理 文章目录 0. 模型复杂度简介 1. 模型复杂度之一:模型参数量的计算方法 卷积层参数量计算 全连接层参数量计算 2 ...

  9. 卷积层计算量(FLOPS)和参数量的计算

    1.卷积参数量的计算,若卷积层的输入featuremap的维度为Cin×Hin×Win,卷积核的大小为K1×K2, padding=P1×P2, stride=S1×S2,卷积核(filter)的数量 ...

最新文章

  1. 5.1Python函数(一)
  2. python【蓝桥杯vip练习题库】ADV-92求最大公约数(递归)
  3. 修改网站自动关闭时间timeout_Testbench仿真方法2:在Quartus下Testbench编写及脚本文件修改...
  4. vc设置窗口在另一个窗口前面_日常办公如何设置IE
  5. Delphi中DLL初始化和退出处理
  6. boost的chrono模块周期计数延迟的测试程序
  7. 【活动(广州)】office365的开发者训练营
  8. Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final)
  9. IIS反向代理/Rewrite/https卸载配置
  10. 外卖行业现状分析_2019年我国餐饮外卖行业发展现状及前景分析
  11. Session的钝化和活化(序列化和反序列化)
  12. android pad的屏幕纯多少,16:9比例10.1寸屏幕,Galaxy Tab S4可以说是一部好的安卓平板...
  13. vue + echarts 之饼形图
  14. 设计分享|基于51单片机的数字时钟(汇编)
  15. 给hexo搭建的博客更换主题
  16. historic historical
  17. I won't tell you this is about graph theory----zjfc bellman-ford算法与spfa算法
  18. 如何让你的旧电脑跑起来与新电脑一样快?老程序员传授秘诀!
  19. 《Python机器学习及实践》----良/恶性乳腺癌肿瘤预测
  20. Cleanmymac X2023Mac内存清理功能介绍指南

热门文章

  1. html 手机语音聊天,好用的手机语音聊天软件推荐
  2. 幼麟棋牌技术分享系列:H5棋牌游戏加载速度优化
  3. Launcher 记录自定义桌面
  4. 计算机的配件知识,最基本的入门知识:电脑由哪些部件组成?
  5. 【Matlab学习手记】Matlab积分问题
  6. XElement.Load 需要释放吗_蚕茧能清洁毛孔吗 蚕茧护肤的正确方法速Get√|蚕茧|清洁-爱美·BEAUTY...
  7. 什么是用户代理样式表
  8. WinMerge:一个免费开源的文件对比神器
  9. 三段论_五项基本原则
  10. 来客在线客服系统源码 支持一键安装