1 条题解

  • 0
    @ 2026-10-23 18:34:03

    P76006 反转核对 — 题解

    专题: 字符串
    适用题目: 新生 OJ 本地题包中的 P76006,不要直接用于外站同名题。

    解题思路

    倒序遍历第一个字符串,并与第二个字符串从头逐位对应比较。

    复杂度

    时间 O(n)O(n),额外空间 O(1)O(1)(不含输入字符串)。

    易错点

    先检查两串长度是否相同。

    C11 参考程序

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <limits.h>
    
    int main(void) {
        static char a[100005], b[100005];
        scanf("%100004s%100004s", a,b);
        int n = (int)strlen(a), m = (int)strlen(b), ok = n == m;
        for (int i = 0; ok && i < n; i++) if (a[i] != b[n-1-i]) ok = 0;
        puts(ok ? "YES" : "NO");
        return 0;
    }
    

    C++17 参考程序

    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    #define nl '\n'
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        string a,b;
        cin>>a>>b;
        reverse(a.begin(),a.end());
        cout<<(a==b?"YES":"NO")<<nl;
        return 0;
    }
    

    题解建议设置为训练结束后开放。普通判题无法验证是否实际使用指定函数或 STL。

    • 1

    信息

    ID
    1775
    时间
    2000ms
    内存
    256MiB
    难度
    1
    标签
    提交数
    2
    已通过
    1
    上传者