AtCode249 - B - Perfect String

题目地址

B - Perfect String

  • https://atcoder.jp/contests/abc249/tasks/abc249_b

問題描述

Problem Statement

Let us call a string consisting of uppercase and lowercase English alphabets a wonderful string if all of the following conditions are satisfied:

  • The string contains an uppercase English alphabet.
  • The string contains a lowercase English alphabet.
  • All characters in the string are pairwise distinct.

For example, AtCoder and Aa are wonderful strings, while atcoder and Perfect are not.

Given a string SS, determine if SS is a wonderful string.

Constraints

  • 1≤∣S∣≤100
  • S is a string consisting of uppercase and lowercase English alphabets.

Input

Input is given from Standard Input in the following format:

S

Output

If SS is a wonderful string, print Yes; otherwise, print No.


Sample Input 1

AtCoder

Sample Output 1

Yes

AtCoder is a wonderful string because it contains an uppercase English alphabet, a lowercase English alphabet, and all characters in the string are pairwise distinct.


Sample Input 2

Aa

Sample Output 2

Yes

Note that A and a are different characters. This string is a wonderful string.


Sample Input 3

atcoder

Sample Output 3

No

It is not a wonderful string because it does not contain an uppercase English alphabet.


Sample Input 4

Perfect

Sample Output 4

No

It is not a wonderful string because the 22-nd and the 55-th characters are the same.

题意

  • 完美字符串满足
    • 只包含英文
    • 至少包含一个大写和小写
    • 所有文字不重复

思路

  • 循环遍历,定义计数器,使用c++自带的函数统计大写和小写字母数量, 均不为0则满足条件;
    • 不足:这块定义一个bool变量更优
  • 循环时将每位字符存入set结构,因为set里的元素有互异性,所以最后只需要判断set的size是否和输入文字数一致即可。
    • 不足:空间复杂度上升,但处理简单,无脑就把程序写完

题解

小码匠

void coder_solution() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    string s;
    cin >> s;
    set<char> a;
    int big = 0;
    int small = 0;
    for (int i = 0; i < s.size(); i++) {
        if (isupper(s[i])) {
            big++;
        } else if (islower(s[i])){
            small++;
        }
        a.insert(s[i]);
    }
    if (a.size() == s.size() && big > 0 && small > 0) {
        cout << "Yes";
    } else {
        cout << "No";
    }
}

参考题解

void second_solution() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    string s;
    cin >> s;

    sort(s.begin(), s.end());
    for (int i = 0; i < s.length() - 1; i++) {
        if (s[i] == s[i + 1]) {
            cout << "No";
        }
    }

    bool a = false, b = false;
    for (auto &&c: s) {
        if (islower(c)) a = true;
        if (isupper(c)) b = true;
    }
    if (a && b) {
        cout << "Yes" << endl;
    }
}

官方题解

#include <bits/stdc++.h>
using namespace std;
int main() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin >> s;
    bool big = false, small = false;
    for (int i = 0; i < s.size(); i++) {
        if (isupper(s[i])) big = true;
        else small = true;
    }
    bool diff = true;
    for (int i = 0; i < s.size(); i++) {
        for (int j = i + 1; j < s.size(); j++) {
            if (s[i] == s[j]) diff = false;
        }
    }
    if (big && small && diff) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}
  • 仔细分析官方题解,是有可优化空间的。
    • 第一:第8行、9行的判断中,如果bit和small都不为true,其实是没必要走后面循环处理;
    • 第二:第12行、13行用双指针来处理,更好。

待补知识点

  • 依然是函数这块,需要补充。当时看到这个题解,把代码复制到IDE中

    • 一:编译未通过,应该是自己知识水平不够,大神提交代码是没问题,功力不够;

    • 二:模板那块是自己的软肋,还没学习。

    template <class T> T scan() {
        T ret;
        std::cin >> ret;
        return ret;
    }
    
    void best_solution() {
        // 提升cin、cout效率
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
    
        const std::string s = scan<std::string>();
    
        if (s.size() != set(s.begin(), s.end()).size()) {
            std::cout << "No" << endl;
            return;
        }
        bool upp = false;
        bool low = false;
        for (const char c: s) {
            if ('A' <= c && c <= 'Z') {
                upp = true;
            }
            if ('a' <= c && c <= 'z') {
                low = true;
            }
        }
        std::cout << (upp && low ? "Yes" : "No") << endl;
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小码匠和老码农

喜欢作者

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值