Official

B - Daily Cookie 2 Editorial by yuto1115

解説

「現在の SS の中で最も右の位置にある @ を探し、それを . に置き換える」という処理を DD 回行えばよいです。

具体的な実装方法は下記の実装例 (C++, Python) を参考にしてください。

実装例 (C++) :

Copy
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. int n, d;
  5. string s;
  6. cin >> n >> d >> s;
  7. for (int i = 0; i < d; i++) {
  8. for (int j = n - 1; j >= 0; j--) {
  9. if (s[j] == '@') {
  10. s[j] = '.';
  11. break;
  12. }
  13. }
  14. }
  15. cout << s << endl;
  16. }
#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, d;
    string s;
    cin >> n >> d >> s;
    for (int i = 0; i < d; i++) {
        for (int j = n - 1; j >= 0; j--) {
            if (s[j] == '@') {
                s[j] = '.';
                break;
            }
        }
    }
    cout << s << endl;
}

実装例 (Python) :

Copy
  1. n, d = map(int, input().split())
  2. s = input()
  3. for i in range(d):
  4. for j in range(n):
  5. if s[n - 1 - j] == '@':
  6. s = s[:n - 1 - j] + "." + s[n - j:]
  7. break
  8. print(s)
n, d = map(int, input().split())
s = input()
for i in range(d):
    for j in range(n):
        if s[n - 1 - j] == '@':
            s = s[:n - 1 - j] + "." + s[n - j:]
            break
print(s)

posted:
last update:



2025-04-05 (Sat)
21:02:18 +00:00