问题出自这里

问题描述:

Given a string consisting of a,b and c's, we can perform the following operation: Take any two adjacent distinct characters and replace it with the third character. For example, if 'a' and 'c' are adjacent, they can replaced with 'b'. What is the smallest string which can result by applying this operation repeatedly?

For example, you can either get cab -> cc or cab -> bb, resulting in a string of length 2.

For the second case, one optimal solution is: bcab -> aab -> ac -> b. No more operations can be applied and the resultant string has length 1.

我用中文简单重述一下,也就是说字符串中任意两个相邻的不同字符都可以替换为第三种字符,问你一个字符串被多次替换后可以剩下的最短长度为多少.

解答:

这个问题的答案让人拍手称快. 分为三种情况:

首先,如果字符串中字符全部一样,那么直接返回该字符串的长度即可.

其次,如果三种字符的个数都是奇数或者都是偶数,那么答案为2

其他情况,返回1.

解答简单的让你意想不到吧,大家可以试试验证一下答案.

正确性证明:(数学归纳法),N表示字符串的长度

首先,N=3的时候.如果字符都相同,返回3;如果都不同返回2,其他情况返回1.

其次,当N>3的时候,如果字符串中字符不是全部相同的话,我们可以利用替换法则获得长度为N-1的串,且该串也不是由相同字符组成的

还有一个规律,那就是字符个数全为奇数的串经过转换就变成字符个数全为偶数的串,反之亦然。

哦哦,于是乎,我们迭代就可以得到上述结论啦。

附上英文原版:

Use math induction:
1. if you have case of length 3
if all the same then 3
if all different then 1 (i.e. odd counts)
other cases 2
2. Lemma: If string of length N > 3 consist of non same char it always can be reduced to the length N - 1. (prove is simple)
3. Lemma: If string is N > 3 and consist of non same char during reduction to N-1 it may be represented as a string of non the same chars (reader can prove it too as it's not too hard)
4. Lemma: during reduction from N to N-1 (N > 3) if number of chars been all odd then it will become even and reverse. This is super easy to prove.
Now if you take all this lemmas you can easy to see a logic why it works. First you can always reduce a chain of non same chars up to the string with 3 chars, secondary during reduction you'll end up with odd count if you start count have been even or odd.

String Reduction的更多相关文章

  1. String reduction (poj 3401

    String reduction Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1360   Accepted: 447 D ...

  2. String Reduction问题分析

    问题描述: Given a string consisting of a,b and c's, we can perform the following operation: Take any two ...

  3. 第一次作业-----四则运算题目生成(基于java)

    1.题目要求 1.除了整数以外,还要支持真分数的四则运算,真分数的运算,例如:1/6 + 1/8 = 7/24. 2.运算符为 +, −, ×, ÷. 3.并且要求能处理用户的输入,并判断对错,打分统 ...

  4. 10670 Work Reduction (贪心 + 被题意坑了- -)y

    Problem C: Work Reduction Paperwork is beginning to pile up on your desk, and tensions at the workpl ...

  5. OpenCV, color reduction method

    转载请注明出处!!!http://blog.csdn.net/zhonghuan1992 OpenCV, colorreduction method 目标: 这次学习的目标是回答以下的几个问题: 1 ...

  6. uva 10670 Work Reduction(贪心)

    题目连接:10670 - Work Reduction 题目大意:有tol的工作量,和要求达到的工作剩余量sur,然后是公司总数,对应每个公司提供两种服务,1.完成一个工作量,2.完成当前未完成工作量 ...

  7. GPGPU OpenCL Reduction操作与group同步

    Reduction操作:规约操作就是由多个数生成一个数,如求最大值.最小值.向量点积.求和等操作,都属于这一类操作. 有大量数据的情况下,使用GPU进行任务并行与数据并行,可以收到可好的效果. gro ...

  8. 为什么Java中的String是设计成不可变的?(Why String is immutable in java)

    There are many reasons due to the string class has been made immutable in Java. These reasons in vie ...

  9. Educational Codeforces Round 121 (Rated for Div. 2)——B - Minor Reduction

    B - Minor Reduction 题源:https://codeforces.com/contest/1626/problem/B 题意 给定一个超级大的整数 x ,可以对任意相邻两位数进行操作 ...

随机推荐

  1. 判断Check复选框是否选中

    <div id="prm_div" style="font-size: 12px;" align="left"> <for ...

  2. 基于socket的客户端和服务端聊天机器人

    服务端代码如下: using System;using System.Net;using System.Net.Sockets;using System.Text;using System.Threa ...

  3. android线程间通讯

    近来找了一些关于android线程间通信的资料,整理学习了一下,并制作了一个简单的例子. andriod提供了 Handler 和 Looper 来满足线程间的通信.例如一个子线程从网络上下载了一副图 ...

  4. 打造一款属于自己的web服务器——开篇

    JVM总结慢慢来吧,先插播一篇水文,来介绍下最近业余一直在写的一个小项目——easy-httpserver(github).适合新手学习,大神们路过即可^_^. 一.这是个什么玩意? easy-htt ...

  5. Swift给每个开发者赢取500万的机会!不看一生后悔。

    [导语] Swift的横空出世,很多有想法的人已经发现其中的蕴含的巨大商机,而很多新手却只是云里雾里,只知道大家最近讨论Swift很欢乐.内行看门道,外行看热闹,说的就是这个理.如果你能把swift用 ...

  6. 7.FPGA中的同步复位与异步复位

    1.异步复位 always @ ( posedge sclk or negedge s_rst_n ) if ( !s_rst_n ) d_out <= 1'b0; else d_out < ...

  7. 如何在github上fork一个项目来贡献代码以及同步原作者的修改

    [-] 如何贡献自己的力量 如何让自己的项目与原作者的项目保持同步 作为一个IT人,通过github进行学习是最快的成长手 段.我们可以浏览别人的优秀代码.但只看不动手还是成长得很慢,因此为别人贡献代 ...

  8. 如何用pdfbox-app-1.8.10.jar批处理将pdf文档转换成text文档

    1.首先下载pdfbox-app-1.8.10.jar(下载地址:http://pdfbox.apache.org/download.html) 2.将pdfbox-app-1.8.10.jar加载到 ...

  9. 对中级Linux用户有用的20个命令

    1. 命令: Find 搜索指定目录下的文件,从开始于父目录,然后搜索子目录. 注意: -name‘选项是搜索大小写敏感.可以使用-iname‘选项,这样在搜索中可以忽略大小写.(*是通配符,可以搜索 ...

  10. 撸一撸腾讯的微信支付(C#)

    一.前言 以往网上支付都是支付宝的天下,随着微信用户群的日益增多(其实,到现在我也不理解微信为嘛那么火,功能还没QQ强大,或许是公众号的原因?),先如今不上个微信支付你都不好意思说你系统支持在线支付. ...