关闭

Codeforces刷题之路——266A Stones on the Table

标签: codeforcesjava算法
214人阅读 评论(0) 收藏 举报
分类:

A. Stones on the Table
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n stones on the table in a row, each of them can be red, green or blue. Count the minimum number of stones to take from the table so that any two neighboring stones had different colors. Stones in a row are considered neighboring if there are no other stones between them.

Input

The first line contains integer n (1 ≤ n ≤ 50) — the number of stones on the table.

The next line contains string s, which represents the colors of the stones. We'll consider the stones in the row numbered from 1 to nfrom left to right. Then the i-th character s equals "R", if the i-th stone is red, "G", if it's green and "B", if it's blue.

Output

Print a single integer — the answer to the problem.

Examples
input
3
RRG
output
1
input
5
RRRRR
output
4
input
4
BRBG
output
0


题目大意:一排不同颜色的Stones,求解最少需要拿走几块stones才能够相邻的stones不同颜色。

解题思路:水题,暴力扫描。


以下为解题代码(java实现)

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		scanner.nextLine();
		String str = scanner.nextLine();
		char[] ch = str.toCharArray();
	
		int counts = 0;
		for(int i = 0;i < ch.length-1;i++){
			if(ch[i] == ch[i+1]){
				counts++;
			}
		}
		System.out.println(counts);		
		scanner.close();				
	}
}

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

在linux下新增一块硬盘的操作。(包含大于2T的硬盘在linux下挂载操作)

一、安装硬盘到物理机上。(略) 二
  • Season_hangzhou
  • Season_hangzhou
  • 2014-07-02 16:05
  • 16768

线上MYSQL同步报错故障处理总结

http://www.cnblogs.com/itcomputer/articles/5618907.html 前言 在发生故障切换后,经常遇到的问题就是同步报错,数据库很小的时候,d...
  • Steven_liwen
  • Steven_liwen
  • 2016-12-01 17:05
  • 2569

Codeforces 11D A Simple Task 统计简单无向图中环的个数

状态压缩动态规划计算简单无向图中简单环的个数。
  • fangzhenpeng
  • fangzhenpeng
  • 2015-10-12 19:01
  • 1209

小白题解 Codeforces 266A Stones on the Table

小白题解 Codeforces 266A Stones on the Table (LIS问题经典变形)题目链接:点击打开链接 题目大意:给定一排只有三种颜色的n个石头,要求移除掉尽可能少的石头使两...
  • qq512028505
  • qq512028505
  • 2017-05-17 19:28
  • 138

CodeForces-266A-Stones on the Table

A. Stones on the Table time limit per test 2 seconds memory limit per test 256 megabytes...
  • u012188452
  • u012188452
  • 2014-09-10 21:39
  • 281

Codeforces刷题之路—— 266B Queue at the School

B. Queue at the School time limit per test 2 seconds memory limit per test 256 megabytes...
  • CodingXiaoGuo
  • CodingXiaoGuo
  • 2016-08-31 13:55
  • 263

Codeforces刷题之路——116A Tram

A. Tram time limit per test 2 seconds memory limit per test 256 megabytes input st...
  • CodingXiaoGuo
  • CodingXiaoGuo
  • 2016-08-24 15:04
  • 212

POJ 3006 Dirichlet's Theorem on Arithmetic Progressions(我的水题之路——加i个d后的第几个素数)

Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K ...
  • shiow1991
  • shiow1991
  • 2012-02-13 15:24
  • 482

Game of Taking Stones (hdu5973)——2016大连现场赛C题 威佐夫博弈

Two people face two piles of stones and make a game. They take turns to take stones. As game rules, ...
  • xiaohang3414
  • xiaohang3414
  • 2017-06-04 19:21
  • 221

Codeforces Round #266 (Div. 2)E(判断父子关系+离线)

E. Information Graph time limit per test 1 second memory limit per test 512 megabytes ...
  • cq_phqg
  • cq_phqg
  • 2014-09-15 22:32
  • 914
    个人资料
    • 访问:10031次
    • 积分:333
    • 等级:
    • 排名:千里之外
    • 原创:23篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条