0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ABC - 033- A&B&C

Last updated at Posted at 2019-04-26

AtCoder ABC 033 A&B&C

AtCoder - 033

A問題

	private void solveA() {
		int numN = nextInt();
		if (numN == 0) {
			out.println("SAME");
			return;
		}
		int res = numN % 10;
		numN /= 10;
		int cnt = 1;
		while (numN > 0 && res == numN % 10) {
			numN /= 10;
			cnt++;
		}
		System.out.println(cnt == 4 ? "SAME" : "DIFFERENT");
	}

B問題

private void solveB() {
		int numN = nextInt();
		String[] names = new String[numN];
		int[] people = new int[numN];
		long total = 0;
		for (int i = 0; i < numN; i++) {
			names[i] = next();
			people[i] = nextInt();
			total += people[i];
		}
		total = (long) Math.ceil(total / 2);
		for (int i = 0; i < people.length; i++) {
			if (people[i] > total) {
				System.out.println(names[i]);
				return;
			}
		}

		out.println("atcoder");
	}

C問題:split Version

  • 最初に思いついた実装
    • +で区切る
    • 区切った中は*または単純な数値なので、一つでも$0$があったら全体が$0$になる
      • なので、区切った中の$0$の有無を確認し、0が無かったら1個置き換える
	 /*
	 * 文字分割を利用したversion
	 */
	private void solveC2() {
		String wk = next();
		String[] multiply = wk.split("\\+");

		int res = 0;
		for (int i = 0; i < multiply.length; i++) {
			String[] inner = multiply[i].split("\\*");
			boolean inZero = false;
			for (int j = 0; j < inner.length; j++) {
				String string = inner[j];
				if (string.equals("0")) {
					inZero = true;
				}
			}
			if (!inZero) {
				res++;
			}
		}

		out.println(res);
	}

C問題:しゃくとり法Version

  • 方針は上のsplitと同じ

勉強のために、split以外での実装を試す。
しゃくとり法の説明は下記サイトを読めばわかる。
ABC038-Cでしゃくとり法勉強したけど、未だ解説読みながらじゃないと微妙

参考サイト:しゃくとり法 (尺取り法) の解説と、それを用いる問題のまとめ

	/*
	 * しゃくとり法version
	 */
	private void solveC() {
		String wk = next();

		int res = 0;
		int left = 0;
		int right = 0;
		boolean inZero = false;
		while (left <= wk.length()) {

			/*
			 * '+'が出現するまで移動
			 * 一度見つけたら、左と右の位置が揃うまで右の移動は保留
			 */
			while (right < wk.length() && wk.charAt(right) != '+') {
				right++;
				//右を動かし始めたのでゼロリセット
				inZero = false;
			}

			/*
			 * 左を動かしている最中、最後までの間に'0'が出現したら発見
			 */
			if (left < wk.length() && wk.charAt(left) == '0') {
				inZero = true;
			}

			/*
			 * 左右の位置が揃ったときに、
			 * '0'が出現していなかったら
			 * 一個の数値を0に置換する
			 * ゼロが見つかっていないことにする
			 */
			if (left == right && !inZero) {
				res++;
				inZero = false;
			}
			/*
			 * これで左右の位置が揃った場合は右をずらす
			 * 揃っていないときは左を右に近づける
			 */
			if (left == right) {
				right++;
			} else {
				left++;
			}
		}
		out.println(res);
	}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?