CodeForces-266A-Stones on the Table

</pre></div><div class="time-limit" style="margin:0px auto; padding:0px"><div class="property-title" style="margin:0px; padding:0px 4px 0px 0px; display:inline"><span style="color: rgb(34, 34, 34); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 20.9090900421143px; line-height: 21px; text-align: center;">Stones on the Table</span></div></div><div class="time-limit" style="margin:0px auto; padding:0px"><div class="property-title" style="margin:0px; padding:0px 4px 0px 0px; display:inline">time limit per test</div>2 seconds</div><div class="memory-limit" style="margin:0px auto; padding:0px"><div class="property-title" style="margin:0px; padding:0px 4px 0px 0px; display:inline">memory limit per test</div>256 megabytes</div><div class="input-file" style="margin:0px auto; padding:0px"><div class="property-title" style="margin:0px; padding:0px 4px 0px 0px; display:inline">input</div>standard input</div><div class="output-file" style="margin:0px auto; padding:0px"><div class="property-title" style="margin:0px; padding:0px 4px 0px 0px; display:inline">output</div>standard output</div></div><div style="margin:0px; padding:0px"><p style="margin-top:1.5em; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:1em; line-height:1.4em">There are <span class="tex-span" style="font-size:17.27272605896px; font-family:'times new roman',sans-serif"><em>n</em></span> 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.</p></div><div class="input-specification" style="margin:0px; padding:0px"><div class="section-title" style="margin:0px; padding:0px; font-size:16.3636360168457px; font-weight:bold">Input</div><p style="margin-top:1.5em; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:1em; line-height:1.4em">The first line contains integer <span class="tex-span" style="font-size:17.27272605896px; font-family:'times new roman',sans-serif"><em>n</em></span> <span class="tex-span" style="font-size:17.27272605896px; font-family:'times new roman',sans-serif">(1 ≤ <em>n</em> ≤ 50)</span> — the number of stones on the table.</p><p style="margin-top:1.5em; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:1em; line-height:1.4em">The next line contains string <span class="tex-span" style="font-size:17.27272605896px; font-family:'times new roman',sans-serif"><em>s</em></span>, which represents the colors of the stones. We'll consider the stones in the row numbered from<span class="tex-span" style="font-size:17.27272605896px; font-family:'times new roman',sans-serif">1</span> to <span class="tex-span" style="font-size:17.27272605896px; font-family:'times new roman',sans-serif"><em>n</em></span> from left to right. Then the <span class="tex-span" style="font-size:17.27272605896px; font-family:'times new roman',sans-serif"><em>i</em></span>-th character <span class="tex-span" style="font-size:17.27272605896px; font-family:'times new roman',sans-serif"><em>s</em></span> equals "<span class="tex-font-style-tt" style="font-size:15.4545450210571px; font-family:'courier new',monospace">R</span>", if the <span class="tex-span" style="font-size:17.27272605896px; font-family:'times new roman',sans-serif"><em>i</em></span>-th stone is red, "<span class="tex-font-style-tt" style="font-size:15.4545450210571px; font-family:'courier new',monospace">G</span>", if it's green and "<span class="tex-font-style-tt" style="font-size:15.4545450210571px; font-family:'courier new',monospace">B</span>", if it's blue.</p></div><div class="output-specification" style="margin:0px 0px 1em; padding:0px"><div class="section-title" style="margin:0px; padding:0px; font-size:16.3636360168457px; font-weight:bold">Output</div><p style="margin-top:1.5em; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:1em; line-height:1.4em">Print a single integer — the answer to the problem.</p></div><div class="sample-tests" style="margin:0px; padding:0px; font-family:Consolas,'Lucida Console','Andale Mono','Bitstream Vera Sans Mono','Courier New',Courier; font-size:0.9em"><div class="section-title" style="margin:0px; padding:0px; font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14.5454540252686px; font-weight:bold">Sample test(s)</div><div class="sample-test" style="margin:0px; padding:0px"><div class="input" style="margin:0px; padding:0px; border:1px solid rgb(136,136,136)"><div class="title" style="margin:0px; padding:0.25em; font-size:1.3em; border-bottom-width:1px; border-bottom-style:solid; border-bottom-color:rgb(136,136,136); text-transform:lowercase; font-weight:bold">input</div><pre style="margin-top:0px; margin-bottom:0px; padding:0.25em; font-size:12.7272720336914px; font-family:Consolas,'Lucida Console','Andale Mono','Bitstream Vera Sans Mono','Courier New',Courier; line-height:1.25em; white-space:pre-wrap; word-wrap:break-word; color:rgb(136,0,0); background-color:rgb(239,239,239)">3
RRG
output
1
input
5
RRRRR
output
4
input
4
BRBG
output
0

import java.util.*;

public class StonesOnTheTable {
	public static void main(String[] args) {
		Scanner inScanner = new Scanner(System.in);
		int n = inScanner.nextInt();
		String tempString = inScanner.next();
		List list = new ArrayList();
		char[] x = tempString.toCharArray();
		for (int i = 0; i < n; i++) {
			list.add(x[i]);
		}
		// preparing work for transform String to List//remember that when
		// remove the element in the list, the set that deleted will be filled
		// automatically
		int temp = 0, count = 0;// ini

		for (int i = 0; i < list.size() - 1; i++) {
			temp = i + 1;// update//ini
			while (temp < list.size()) {
				if (list.get(i).equals(list.get(temp))) {
					list.remove(temp);
					count++;
				} else
					break;
			}
		}
		System.out.println(count);
	}
}

       最初的思路在于依次判断小球右侧球颜色是否相同,若相同则删除,然后判断下个相邻球是否仍旧同色,若同色则继续删除,若不同色则跳到下个小球进行判断,依次同理。需要注意的是List数据结构在小球被删除后,索引之后的小球会自动将被删小球的位置填充,所以temp不需要进行人为增加。当发现相邻小球颜色不一的时候,就立刻跳出循环。

       但写完后发现思考不周全,方法繁琐。题目只要求输出最小次数,并无要求得出最后符合要求的颜色序列,只要将题意简化为依次计算每个小球右侧颜色一样的小球即可。

import java.util.*;
public class Stones {
	public static void main(String[] args) {
		Scanner kbd = new Scanner(System.in);
		int n = kbd.nextInt();
		String s = kbd.next();
		int r = 0;
		for(int i = 1;i<s.length();i++) {
			if(s.charAt(i-1)==s.charAt(i)) {
				r++;
			}
		}
		System.out.println(r);
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值