a223簡單字串、字元與格式解析

TOI 2022/01 新手組:雜訊移除

刪掉指定數字字元,保持其餘字元原順序再鏡像比較。

前往題目練習 →回報教學錯誤時間 O(|T|),空間 O(|T|)。

這題在學什麼

T 應以字串讀入,指定的 N 也以一字元處理。

逐字保留不等於 N 的字元,得到刪除雜訊後的字串。

將保留字串與反轉結果比較,相等輸出 Yes,否則輸出 No;前導零不能丟失。

Step-by-step animation

雜訊移除・保留字元後判迴文

回報動畫錯誤
步驟 1 / 3

指定雜訊

8081708 中所有 8 都要刪除。

8
0
8
1
7
0
8
noise
8

Reference solution

回報程式錯誤

完整程式

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string t, n, s;
    cin >> t >> n;
    for (char c : t)
        if (c != n[0])
            s += c;
    string rev = s;
    reverse(rev.begin(), rev.end());
    cout << (s == rev ? "Yes" : "No") << '\n';
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多