栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > C/C++/C#

AtCode ABC249 - B - Perfect String

C/C++/C# 更新时间:发布时间: 百科书网 趣学号
标签
  • 集合
题目地址

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 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 
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  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();
    
        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;
    }
    
转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/869101.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号