Fewest Flops

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]



评价:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值