pat题库--字符串

这篇博客主要探讨了PAT(Peking University Automatic Testing System)题库中与字符串相关的题目,包括1071 Speech Patterns、1061 Dating等,讲解了解题思路和策略,帮助读者提升字符串处理和算法能力。

1071 Speech Patterns


#include<bits/stdc++.h>
using namespace std;

bool check(char c)
{
    if('a'<=c && c<='z')
        return true;
    if('A'<=c && c<='Z')
        return true;
    if('0'<=c && c<='9')
        return true;
    return false;
}

int main()
{
    string s;
    getline(cin,s);

    unordered_map<string,int> mp;

    string res;

    for(int i=0; i<s.size();i++)
        if(check(s[i]))
        {
            int j=i;
            string word;
            while(j<s.size() && check(s[j]))
                word+=towlower(s[j++]);
            i=j;
            mp[word]++;
        }

    string word;
    int cnt=-1;

    for(auto item:mp)
    {
    	//这里不能写成item.second>=cnt && word<item.first因为见下图
        if(item.second>cnt || item.second==cnt && word<item.first)
        {
            cnt=item.second;
            word=item.first;
        }

      cout << word << " " << cnt << endl;
    }


    return 0;
}

在这里插入图片描述
1061 Dating

/*
1.星期几,字符串1和字符串2相同的字母  ABCDEFG
2.小时 是 字符串1和字符串2相同的字符(这个字符包括0~9和A~N)并且记住一定是在上面表示星期几的后面对应的字符
3.分钟 是 字符串3和字符串4所对应的字符(这个字符包括小写和大写字母)


*/

#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    string a, b, c, d;
    cin >> a >> b >> c >> d;

    int k = 0;
    while (true)
    {
        if (a[k] == b[k] && a[k] >= 'A' && a[k] <= 'G') break;
        k ++ ;
    }

    char weekdays[7][4] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
    printf("%s ", weekdays[a[k] - 'A']);

    k ++ ;
    while (true)
    {
        if (a[k] == b[k] && (a[k] >= '0' && a[k] <= '9' || a[k] >= 'A' && a[k] <= 'N')) break;
        k ++ ;
    }

    printf("%02d:", a[k] <= '9' ? a[k] - '0' : a[k] - 'A' + 10);

    for (int i = 0;; i ++ )
        if (c[i] == d[i] && (c[i] >= 'a' && c[i] <= 'z' || c[i] >= 'A' && c[i] <= 'Z'))
        {
            printf("%02d\n", i);
            break;
        }

    return 0;
}


PAT Ranking of Institutions

这道题有个大坑就是我们计算总分的时候是double类型,将其放到res中时,是将他们转换成int类型来比较的,但是会出现精度问题,所以在放的时候加上1e-8
#include <iostream>
#include <cstring>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;

struct School
{
    string name;
    int cnt=0;
    double sum=0;

    bool operator< (const School &t) const
    {
        if (sum != t.sum) return sum > t.sum;
        if (cnt != t.cnt) return cnt < t.cnt;
        return name < t.name;
    }
};

int main()
{
    int n;
    cin >> n;

    unordered_map<string, School> hash;
    while (n -- )
    {
        string id, sch;
        double grade;
        cin >> id >> grade >> sch;

        for (auto& c : sch) c = tolower(c);

        if (id[0] == 'B') grade /= 1.5;
        else if (id[0] == 'T') grade *= 1.5;

        hash[sch].sum += grade;
        hash[sch].cnt ++ ;
        hash[sch].name = sch;
    }

    vector<School> schools;
    for (auto item : hash)
    {
        item.second.sum = (int)(item.second.sum + 1e-8);
        schools.push_back(item.second);
    }

    sort(schools.begin(), schools.end());
    cout << schools.size() << endl;

    int rank = 1;
    for (int i = 0; i < schools.size(); i ++ )
    {
        auto s = schools[i];
        if (i && s.sum != schools[i - 1].sum) rank = i + 1;
        printf("%d %s %d %d\n", rank, s.name.c_str(), (int)s.sum, s.cnt);
    }

    return 0;
}


Decode Registration Card of PAT

这道题蛋疼的是对于最后一种查询,如果是空则优先输出,否则再依次输出结果,这样就不会超时
#include <iostream>
#include <cstring>
#include <unordered_map>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 10010;

int n, m;
struct node
{
    string id;
    int grade;

    bool operator< (const node &t) const
    {
        if (grade != t.grade) return grade > t.grade;
        return id < t.id;
    }
}Node[N];

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i ++ ) cin >> Node[i].id >> Node[i].grade;

    for (int k = 1; k <= m; k ++ )
    {
        string t, c;
        cin >> t >> c;

        printf("Case %d: %s %s\n", k, t.c_str(), c.c_str());
        if (t == "1")
        {
            vector<node> persons;
            for (int i = 0; i < n; i ++ )
                if (Node[i].id[0] == c[0])
                    persons.push_back(Node[i]);

            sort(persons.begin(), persons.end());

            if (persons.empty()) puts("NA");
            else
                for (auto person : persons) printf("%s %d\n", person.id.c_str(), person.grade);
        }
        else if (t == "2")
        {
            int cnt = 0, sum = 0;
            for (int i = 0; i < n; i ++ )
                if (Node[i].id.substr(1, 3) == c)
                {
                    cnt ++ ;
                    sum += Node[i].grade;
                }

            if (!cnt) puts("NA");
            else printf("%d %d\n", cnt, sum);
        }
        else
        {
            unordered_map<string, int> hash;
            for (int i = 0; i < n; i ++ )
                if (Node[i].id.substr(4, 6) == c)
                    hash[Node[i].id.substr(1, 3)] ++ ;

            vector<pair<int, string>> rooms;
            for (auto item : hash) rooms.push_back({-item.second, item.first});

            sort(rooms.begin(), rooms.end());
            if (rooms.empty()) puts("NA");
            else
                for (auto room : rooms)
                    printf("%s %d\n", room.second.c_str(), -room.first);
        }
    }

    return 0;
}


### PAT程序设计乙级考试概述 PAT(Programming Ability Test)程序设计乙级考试旨在评估考生的基础编程能力和解决简单问题的能力。对于希望参加2024年PAT乙级考试的考生来说,了解考试大纲、备考资料以及练习题的选择至关重要。 #### 考试大纲 PAT乙级考试主要考察以下几个方面: 1. **基本语法和语义** - 熟练掌握C/C++/Java等常用编程语言的基本语法和语义。 2. **基础数据结构** - 掌握数组、链表、栈、队列等常见数据结构的操作[^2]。 3. **算法基础** - 理解并能够编写简单的排序算法(如冒泡排序、快速排序)、查找算法(如二分查找),以及其他常见的基础算法。 4. **输入输出处理** - 能够正确处理标准输入输出,并且熟悉文件操作。 5. **字符串处理** - 使用`ctype.h`中的函数如`strlwr()`将字符串转换为小写,使用`strupr()`将字符串转换为大写,在C++中可以使用`tolower()`和`toupper()`配合`transform()`函数来实现相同功能[^3]。 6. **调试技巧** - 学会利用编译器自带工具或第三方插件进行代码调试。 #### 备考资料推荐 为了更好地准备PAT乙级考试,可以选择以下几类资源作为辅助学习材料: - **官方文档与教程**:仔细研读所选编程语言的官方文档,这是最权威的学习指南之一。 - **在线课程平台**:像Coursera、edX这样的平台上提供了大量优质的计算机科学入门课程,可以帮助巩固理论知识。 - **书籍教材**: - 《C Primer Plus》适合初学者深入理解C语言; - 对于更高级的内容,《Introduction to Algorithms》是一本经典的算法教科书。 - **刷题网站**:LeetCode、牛客网等都是很好的实践场所,上面有大量的题目供练习,特别是针对PAT题库的专项训练尤为重要。 #### 练习题建议 根据陈越教授的经验分享,合理的练习计划应该包括但不限于以下几点: - **时间管理**:能够在规定时间内高效解决问题是非常重要的技能。例如,争取在10分钟内完成一道15分的小题;半小时内搞定一题价值20分的大题;而面对难度更高的25分题,则需控制在45分钟左右的时间范围内[^1]。 - **逐步提升难度**:从较为容易的问题开始做起,随着自信心和技术水平的增长逐渐挑战更高层次的任务。 - **注重质量而非数量**:与其盲目追求数量上的优势,不如花更多心思去理解和优化每一个解答过程,确保每一步都扎实可靠。 ```cpp // C++ 示例代码展示如何改变大小写字母 #include <algorithm> #include <iostream> int main(){ std::string str = "HelloWorld"; // 小写转大写 transform(str.begin(), str.end(), str.begin(), [](unsigned char c){ return toupper(c); }); std::cout << str << "\n"; // 输出 HELLO WORLD // 大写转小写 transform(str.begin(), str.end(), str.begin(), [](unsigned char c){ return tolower(c); }); std::cout << str << "\n"; // 输出 hello world return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值