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 - 058- A&B&C

Posted at

AtCoder ABC 058 A&B&C

AtCoder - 058

A問題

  • $A-B=B-C$
	private void solveA() {
		int numA = nextInt();
		int numB = nextInt();
		int numC = nextInt();

		if (numB - numA == numC - numB) {
			out.println("YES");
		} else {
			out.println("NO");
		}

		out.println("");
	}

B問題

  • 文字列Oと文字列Eを結合する
    • 偶数の時は文字列Oから、奇数の時は文字列Eから1文字ずつ取り出して結合する(実装だと逆になってるけど気にしない)
	private void solveB() {
		String strO = next();
		String strE = next();

		StringBuilder builder = new StringBuilder();
		IntStream.range(0, strO.length() + strE.length()).reduce(0, (sum, i) -> {
			if (i % 2 == 0) {
				builder.append(strO.charAt(sum));
			} else {
				builder.append(strE.charAt(sum));
				sum++;
			}
			return sum;
		});

		//		int cnt = 0;
		//		for (int i = 0; i < strO.length() + strE.length(); i++) {
		//			if (i % 2 == 0) {
		//				builder.append(strO.charAt(cnt));
		//			} else {
		//				builder.append(strE.charAt(cnt));
		//				cnt++;
		//			}
		//		}
		out.println(builder.toString());
	}

C問題

  • 全ての$文字列_i$に出現する文字を抽出
    • その文字の最小出現数を取得
      • 全ての$文字列_i$に共通している文字数は最小出現数
    • 文字を結合して、文字コード順にソートすればよい
  • 文字を抽出する際に、文字列ごとに'a'-'z'の配列を生成し、それぞれいくつあるのかを数えればよい
    • 存在しないcharは0となるので、最小出現数を数えた時に0をカウントできる

配列の生成サンプル

文字列 a b c d e f g h i j k l m n o p q r s t u v w x y z
cbaa 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
daacc 2 0 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
acacac 3 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
	private void solveC() {
		int numN = nextInt();
		/**
		 * 読み込みをStreamを使ってみてる
		 */
		String[] wk = IntStream.range(0, numN).mapToObj(i -> next()).toArray(String[]::new);

		int[][] array = new int[numN][26];

		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < wk[i].length(); j++) {
				int alphaIndex = wk[i].charAt(j) - 'a';
				array[i][alphaIndex] += 1;
			}
		}

		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < 26; i++) {
			int res = Integer.MAX_VALUE;
			for (int j = 0; j < array.length; j++) {
				res = Math.min(res, array[j][i]);
			}
			for (int j = 0; j < res; j++) {
				builder.append((char) (i + (int) 'a'));
			}
		}

		out.println(builder.toString());
	}
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?