1 条题解

  • 0
    @ 2026-10-23 18:33:59

    P76004 忽略大小写比较 — 题解

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

    解题思路

    先把两行字符串都转换为小写,再按字典序比较,最后统一输出 -1、0 或 1。

    复杂度

    时间 O(n)O(n),空间 O(n)O(n)。

    易错点

    不要直接比较原字符串,否则大小写会影响排序。

    C11 参考程序

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <limits.h>
    
    int main(void) {
        char a[1005], b[1005]; scanf("%1004s%1004s", a, b);
        for (int i = 0; a[i]; i++) a[i] = (char)tolower((unsigned char)a[i]);
        for (int i = 0; b[i]; i++) b[i] = (char)tolower((unsigned char)b[i]);
        int x = strcmp(a,b);
        printf("%d\n", (x>0)-(x<0));
        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;
        for(char &c:a)c=tolower(c);
        for(char &c:b)c=tolower(c);
        cout<<(a<b?-1:a>b?1:0)<<nl;
        return 0;
    }
    

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

    • 1

    信息

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