๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[Codeforces] 1303A: Erasing Zeroes

by ์ฃผ๋ฐœ2 2020. 3. 16.
๋ฐ˜์‘ํ˜•

https://codeforces.com/problemset/problem/1303/A

 

Problem - 1303A - Codeforces

 

codeforces.com

์ฝ”๋“œ

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int t = scan.nextInt();
		for(int tc=0; tc<t; tc++) {
			String s = scan.next();
			int zero = 0;
			int first = s.indexOf("1");
			int last = s.lastIndexOf("1");
			if(first == last) {
				System.out.println("0");
				continue;
			}
			for(int i=first; i<=last; i++) {
				if(s.charAt(i) == '0')
					zero ++;
			}
			System.out.println(zero);
		}
		scan.close();
	}
}

ํ’€์ด

์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์—์„œ ๋ชจ๋“  1์ด ํ•˜์œ„ ์„ธ๊ทธ๋จผํŠธ๋ฅผ ํ˜•์„ฑํ•˜๋„๋ก 0์„ ์ง€์šฐ๊ธฐ.

100101 -> 0์„ ๋ชจ๋‘ ์ง€์›Œ์•ผํ•จ

010110 -> 0์„ ํ•œ๊ฐœ๋งŒ ์ง€์šฐ๋ฉด ๋จ

i.e) ๋ชจ๋“  ๋ฌธ์ž์—ด์ด 1์˜ ์—ฐ์†์ด์–ด์•ผํ•จ.

 

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ 1์ด ์‹œ์ž‘ํ•˜๋Š” ์ธ๋ฑ์Šค์™€ ๋๋‚˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š”๋‹ค.

 - indexOf , lastIndexOf

 

๊ทธ ํ›„ ์‹œ์ž‘ ์ธ๋ฑ์Šค์™€ ๋ ์ธ๋ฑ์Šค๊ฐ€ 1์ธ ๋ฌธ์ž์—ด ์‚ฌ์ด์—์„œ 0์˜ ๊ฐฏ์ˆ˜๋ฅผ ์นด์šดํŠธ ํ•œ๋‹ค.

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€