PAT甲级官网 刷题(6)

PAT1039 Course List for Student

  先来一道签到题,需要使用map来把string映射到int,然后使用vector邻接表存储。

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

#define ac cin.tie(0);cin.sync_with_stdio(0);

using namespace std;
vector<int> G[400010];
unordered_map<string, int> map;
int n, m, cnt;

int main() {
    ac
    int id, len;
    string s;

    cin >> n >> m;
    while (m--) {
        cin >> id >> len;
        while (len--) {
            cin >> s;
            //没找到
            if (map.find(s) == map.end())
                map[s] = cnt++;
             //存边
            G[map[s]].push_back(id);
        }
    }

    for (int i = 0; i < cnt; i++)
    //排序
        sort(G[i].begin(), G[i].end());
    while (n--) {
        cin >> s;
        if (map.find(s) != map.end()) {
            cout << s << " " << G[map[s]].size();
            for (int i:G[map[s]])
                cout << " " << i;
        } else
            cout << s << " 0";
        cout << endl;
    }
    return 0;
}


PAT1093 Count PAT’s

  说是使用动态规划解,但是用了简便的方法,做了预处理就解决了,PAT动态规划不怎么考。

#include<iostream>
#include<string>

using namespace std;
const int MAXN = 100010;
const int MOD = 1000000007;
//记录P的个数   记录T的个数
int le[MAXN], ri[MAXN];

int main() {
    register int ans = 0;
    string s;

    cin >> s;
    for (int i = 0; i < s.length(); i++) {
        if (i)
            le[i] += le[i - 1];
        le[i] += s[i] == 'P';
    }
    for (int i = s.length() - 1; i >= 0; i--) {
        if (i < s.length() - 1)
            ri[i] += ri[i + 1];
        ri[i] += s[i] == 'T';
    }

    for (int i = 0; i < s.length(); i++)
    	//遇到A,就把它前面遇到的P和后面遇到的T相乘
        if (s[i] == 'A') {
            ans += le[i] * ri[i];
            ans %= MOD;
        }
    cout << ans;
    return 0;
}

PAT1045 Favorite Color Stripe

  这道题不得不用DP了,dp掌握的不牢,代码是别人写的,学!

#include <iostream>

using namespace std;

const int N = 210, M = 10010;

int n, m, l;
int p[N], s[M];
int f[N][M];

int main() {
    cin >> n;

    cin >> m;
    for (int i = 1; i <= m; i++) cin >> p[i];

    cin >> l;
    for (int i = 1; i <= l; i++) cin >> s[i];

    for (int i = 1; i <= m; i++)
        for (int j = 1; j <= l; j++) {
        	//前一个位置同一个颜色或者当前位置前一个颜色
            f[i][j] = max(f[i - 1][j], f[i][j - 1]);
            //如果颜色相同,那么额外考虑前一个位置当前颜色+1
            if (p[i] == s[j]) f[i][j] = max(f[i][j], f[i][j - 1] + 1);
        }

    cout << f[m][l] << endl;

    return 0;
}

PAT1147 Heaps

  判断是否为堆,因为是完全二叉树实现的,所以 n*2就是节点n的左孩子。

#include<iostream>

#define ac cin.tie(0);cin.sync_with_stdio(0);

using namespace std;
const int MAXN = 1010;
int arr[MAXN], n, m;

void dfs(const int root) {
    if (root > n)
        return;
    dfs(2 * root);
    dfs(2 * root + 1);
    cout << arr[root];
    if (root != 1)
        cout << " ";
}

int main() {
    ac
    cin >> m >> n;
    while (m--) {
        for (int i = 1; i <= n; i++)
            cin >> arr[i];
        bool op1 = false, op2 = false;
        for (int i = 1; i <= n; i++) {
        	//左孩子存在
            if (2 * i <= n) {
                if (arr[i] >= arr[2 * i])
                    op1 = true;
                if (arr[i] <= arr[2 * i])
                    op2 = true;
            }
            //右孩子存在
            if (2 * i + 1 <= n) {
                if (arr[i] >= arr[2 * i + 1])
                    op1 = true;
                if (arr[i] <= arr[2 * i + 1])
                    op2 = true;
            }
        }
        //说明有的孩子节点比父结点大,有的比父结点小,不是堆
        if (op1 && op2)
            cout << "Not Heap" << endl;
        else if (op1)
            cout << "Max Heap" << endl;
        else
            cout << "Min Heap" << endl;
        dfs(1);
        if (m)
            cout << endl;
    }

    return 0;
}

PAT1140 Look-and-say Sequence

  模拟,首先得理解题意><

#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

int main() {
    int k, cnt, pos;
    string s, tmp;

    cin >> s >> k;
    for (int i = 2; i <= k; i++) {
        pos = 0;
        while (pos < s.length()) {
        	//cnt统计字符出现的次数
            cnt = 1;
            while (pos + 1 < s.length() && s[pos] == s[pos + 1])
                ++pos, ++cnt;
            tmp += s[pos];
            tmp += to_string(cnt);
            ++pos;
        }
        //交换字符串
        s = tmp;
        tmp = "";
    }
    cout << s;
    return 0;
}

冲冲冲!
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值