pat甲级真题分类--字符串处理

本文涵盖了多个编程挑战的解决方案,包括寻找最短子序列、查找特定字符的单词、解码注册卡信息以及生成Look-and-say序列。同时,还涉及到了微博抽奖算法,讨论了如何避免重复抽取已中奖的用户。这些题目涉及字符串处理、排序、数据结构和算法应用,是提升编程技能的良好实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1

7-2 Subsequence in Substring (25 分)


#include<iostream>
using namespace std;

const int maxn=10010;

int minlen=maxn;

string s1,s2,res;

int main()
{
    cin >> s1 >> s2;
    int len1=s1.size();
    int len2=s2.size();

    for(int i=0; i<len1; i++)
    {
        if(s1[i]==s2[0])
        {
            int t=1,j=i+1;
            for(; j<len1; j++)
            {
                if(s1[j]==s2[t])
                    t++;
                if(t==len2)
                    break;
            }

            if(t==len2)
            {
                if(j-i+1<minlen)
                {
                    minlen=j-i+1;
                    res=s1.substr(i,minlen);
                }
            }
        }
    }
    cout << res;

    return 0;
}

2

7-1 Good in C (20分)

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector<string> v[26], word;     //word存放单词,不包括标点
	string s, temp;

	for(int i = 0; i < 26; i++)
	{
		for(int j = 0; j < 7; j++)
		{
			getline(cin, s);
			v[i].push_back(s);
		}
	}


	getline(cin, s);            //读入要查询的单词
	for(int i = 0; i < s.length(); i++)
    {
		if(s[i] <= 'Z' && s[i] >= 'A')          //如果是字母将其放入temp中
		{
			temp += s[i];
		}
        else                //如果不是字母
        {
			if(temp != "")      //如果temp不是空
			{
				word.push_back(temp);       //将temp放到word中
				temp = "";      //将temp改为空
			}
		}
	}


	for(int i = 0; i < word.size(); i++)            //遍历word中的每个单词,比如第1个是HELLO
    {
		for(int j = 0; j < 7; j++)          //这明显的是遍历7行
		{
			for(int k = 0; k < word[i].size(); k++)         //遍历每个单词中的字母,比如HELLO中的第一个单词是H
			{           //这个是先整体输出完一行再输出下一行
				int ind = word[i][k] - 'A';         //ind代表具体的那个单词
				printf("%s%s", k == 0?"":" ", v[ind][j].c_str());       //因为v的特殊性第1个空放的是第几个单词,第2个才是遍历的行数
			}
			if( !(i == word.size() - 1 && j == 6) )         //这个控制的是每一行之间的空格
                printf("\n");   //注意这里并不是在中间换了两行,而是在第一个单词中,输出完最后一行,令起一行,完了下面的代码在空一行,才有了结果所示
		}

		if(i != word.size() - 1)        //这个控制的每个单词之间的空格
            printf("\n");
	}
	return 0;
}

3

7-2 Decode Registration Card of PAT (25 分)

#include <iostream>
#include <cstring>
#include <unordered_map>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 10010;

//注意输出都用printf

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

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

int main()
{
    cin >> n >> m;      //输入n和m
    
    for (int i = 0; i < n; i ++ ) cin >> p[i].id >> p[i].grade;     //输入编号和成绩

    for (int k = 1; k <= m; k ++ )          //一共m个询问
    {
        string t, c;
        cin >> t >> c;         

        printf("Case %d: %s %s\n", k, t.c_str(), c.c_str());
        if (t == "1")
        {
            vector<Person> persons;
            for (int i = 0; i < n; i ++ )
                if (p[i].id[0] == c[0])
                    persons.push_back(p[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")          //如果是第2中类型
        {
            int cnt = 0, sum = 0;
            for (int i = 0; i < n; i ++ )
                if (p[i].id.substr(1, 3) == c)
                {
                    cnt ++ ;
                    sum += p[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 (p[i].id.substr(4, 6) == c)
                    hash[p[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;
}


4

A Look-and-say Sequence (20 分)

#include<iostream>
#include<cstring>

using namespace std;

int main()
{
    int d,n;
    cin>>d>>n;      //读入一个d,一个n

    string cur=to_string(d);
    
    for(int k=0;k<n-1;k++)
    {
        string next;
        for(int i=0;i<cur.size();)
        {
            int j=i+1;
            while(j<cur.size()&&cur[i]==cur[j]) j++;
            next+=cur[i]+to_string(j-i);
            i=j;
        }
        cur=next;
    }

    cout<<cur<<endl;

    return 0;
}


5

Raffle for Weibo Followers


#include<iostream>
#include<unordered_set>
using namespace std;

const int maxn=1010;

int m,n,s;

string name[maxn];

int main()
{
    cin >> m >> n >> s;
    for(int i=1; i<=m; i++)
        cin >> name[i];

    unordered_set<string> s1;

    while(s<=m)
    {
        if(s1.count(name[s]))
            s++;
        else
        {
            cout << name[s] << endl;
            s1.insert(name[s]);
            s+=n;
        }
    }
    if(s1.empty())
        cout << "Keep going..." << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值