AtCoder ABC 058 A&B&C
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());
}