编程介的小学生 2017-05-09 09:32 采纳率: 20.5%
浏览 842
已采纳

Popo's Lamps

popo had DPJ (a special Chinese food made of stewed chicken, potato and noodle) with his family every night during the last months of popo's undergraduate time. Along the road to the restaurant where they would have DPJ, there were many lamps. One day, popo looked at one of the lamps while waiting other members of his family, and he noticed there was a magic switch on the lampstand of each lamp, he could choose a number N and a color C in the switch, after turning the switch, the next N lamps to the right of the lamp, including the one where popo turned the switch, would turn to the same color of light that popo chose. popo was so naughty a boy that he wanted to turn the lamps to a pattern of color light to surprise his family.

For example, he would turn the lamps to the pattern red-green-blue-green-red as "RGBGR". It was easy to see that popo could turn the lamp to any pattern he liked, but he wanted to know the minimal number of switches he needed to turn to fulfill the task. It would take him three switching to achieve the above pattern. The first switching would turn the five lamps in red (RRRRR). The second switching would turn the three lamps in the middle to green to obtain the pattern (RGGGR). The final switching would make the lamp in the center blue to obtain pattern he desired (RGBGR).

Input

The input consists of multiple test cases! Each test case begins with a number N (1 <= N <= 50) indicating the length of the pattern. Following this, there is N charaters in a line describing the pattern that popo wanted. Each charater is an uppercase letter indicating the color of lamp in the pattern. You may assume each lamp could turn to any color in the pattern, and the initial state of each lamp was off, which did not show any color. The test cases ends with an N == 0, you need not proceed this input.

Output

For each test case, print the minimal number of switching popo needed to turn the lamps to the pattern he desired in a single line.

Sample Input

5
RGBGR
4
RGRG
7
ABACADA
0
Sample Output

3
3
4

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误