All for PAT秋考 | 1140 - 1143

⚠️不要轻易预分配!

这个错误找了俩小时。。。

  • 定义时声明过vector大小,之后直接给vec[i]赋值就好!!
  • 没有预先分配,用emplace_back (push_back)!!!
  • 对string等c++容器也适用。。。


    ❌❌❌❌❌❌

    改正

1140 Look-and-say Sequence (20 分)

写这题时有个大胆的想法💡:应该只有0 - 9输入,第二个数二分一波……直接输出答案……
😝直接写也是简单的,两个string♻️用,注意最后一种数字和它的个数,并没有在for遍历内加到新字串中,遍历完后记得加上它~

#include <string>
#include <iostream>

using namespace std;

int main() {
    int nn;
    string str, desc_str;
    cin >> str >> nn;
    while (--nn) {
        char curr_ch = str[0];
        int cnt = 1;
        for (int i = 1; i < int(str.length()); ++i) {
            if (str[i] != curr_ch) {
                desc_str += curr_ch;
                desc_str += char(cnt + '0');
                curr_ch = str[i];
                cnt = 1;
            } else cnt++;
        }
        desc_str += curr_ch;
        desc_str += char(cnt + '0');
        str = desc_str;
        desc_str = "";
    }
    cout << str << endl;
    return 0;
}

1141 PAT Ranking of Institutions (25 分)

  • 再次强调,vector预分配空间和push_back/emplace_back不能混用,干脆就别预分配空间啦!预分配和直接按下标赋值是一路的。。。
  • TWS is the total weighted score which is defined to be the integer part of ScoreB/1.5 + ScoreA + ScoreT*1.5, where ScoreX is the total score of the testees belong to this institution on level X; and Ns is the total number of testees who belong to this institution.
    取integer part对浮点数进行强转就好,或者只输出整数位(如果还按浮点数存记得排序时强转。。。所以还是先强转为int再排序好。
  • 若要浮点数四舍五入,➕0.5再强转为int

另外,严格按题目对score的定义,应该是⬇️这样,(下午不知道写了几版

  • 先算各校A、B、T分级别的总分,在✖️系数取整
#include <cstdio>
#include <string>
#include <cstring>
#include <unordered_map>
#include <cctype>
#include <vector>
#include <algorithm>

using namespace std;
unordered_map<string, pair<double, int>> summary;

struct Institution {
    string name;
    int score, num;

    bool operator<(const Institution &i2) const {
        if (score != i2.score) return score > i2.score;
        if (num != i2.num) return num < i2.num;
        return name < i2.name;
    }
};

int main() {
    int nn;
    scanf("%d", &nn);
    char temp_id[10], temp_inst[10];
    int temp_score;
    for (int i = 0; i < nn; ++i) {
        scanf("%s%d%s", temp_id, &temp_score, temp_inst);
        for (int j = 0; j < strlen(temp_inst); ++j) {
            if (isupper(temp_inst[j])) temp_inst[j] += ('a' - 'A');
        }
        double rate;
        switch (temp_id[0]) {
            case 'A':
                rate = 1.0;
                break;
            case 'T':
                rate = 1.5;
                break;
            case 'B':
                rate = 1 / 1.5;
                break;
        }
        summary[temp_inst].first += temp_score * rate;
        summary[temp_inst].second++;
    }
    int n_inst = summary.size();
    vector<Institution> res;
    for (auto &item: summary) {
        res.emplace_back(Institution{item.first, int(item.second.first), item.second.second});
    }
    sort(res.begin(), res.end());
    printf("%d\n", n_inst);
    int rank = 0, score = 0x3fffffff;
    for (int i = 0; i < n_inst; ++i) {
        if (res[i].score < score) {
            rank = i + 1;
            score = res[i].score;
        }
        printf("%d %s %d %d\n", rank, res[i].name.data(), score, res[i].num);
    }
    return 0;
}

1142 Maximal Clique (25 分)

严格照定义来就好!

  • 思路:
    • 先判断给的这一组是不是一个clique(bool flag)。
    • 若是,继续判断有没有clique之外的点和clique内所有点都直接相连(bool more),有则不是最大团,无则是最大团。
  • 复杂度挺高的,为了稍微快一点,用vector和bool in_clique[]存了clique成员,sort一波,避免两次check一对点是否相连。(不知有没有快emmmmm
    判断有无更多点可以放入clique:只要找出一个点和clique内所有点都直接相连就能说明不是最大团了。
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;
bool graph[201][201] = {false};

int main() {
    int nn, mm, nq, t1, t2;
    scanf("%d%d", &nn, &mm);
    for (int i = 0; i < mm; ++i) {
        scanf("%d%d", &t1, &t2);
        graph[t1][t2] = graph[t2][t1] = true;
    }
    scanf("%d", &nq);
    for (int i = 0; i < nq; ++i) {
        bool in_clique[201] = {false};
        vector<int> clique;
        int cnt, v;
        scanf("%d", &cnt);
        for (int j = 0; j < cnt; ++j) {
            scanf("%d", &v);
            clique.emplace_back(v);
            in_clique[v] = true;
        }
        sort(clique.begin(), clique.end());
        bool flag = true;
        for (int k = 0; flag && k < cnt; k++) {
            for (int j = k + 1; j < cnt; ++j) {
                if (!graph[clique[k]][clique[j]]) {
                    flag = false;
                    break;
                }
            }
        }
        if (!flag) {
            puts("Not a Clique");
            continue;
        }
        bool more = false;
        for (int r = 1; !more && r <= nn; ++r) {
            if (!in_clique[r]) {
                int count = 0;
                for (auto item: clique) {
                    if (graph[r][item])
                        count++;
                }
                if (cnt == count) more = true;
            }
        }
        puts(more ? "Not Maximal" : "Yes");
    }
    return 0;
}

1143 Lowest Common Ancestor (30 分)

  • 首先建树,这次突然意识到,一路搜索BST某结点的时候经过的就是这个结点的所有祖先了(祖先也包括它自身),所以建树的时候没必要存father指针域,反正都还要search的……
  • 递归search的时候用vector记录下一路搜索走过的结点。搜索后,若都存在,比较u和v经过的路就好,“分叉”前面的一个就是LCA。
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;
struct Node {
    int key;
    Node *lchild = NULL, *rchild = NULL;
};
int _pre[10001], _in[10001], nn, mm;

Node *createTree(int pst, int ped, int ist, int ied) {
    if (pst > ped || ist > ied) return NULL;
    if (pst == ped && ist == ied)
        return new Node{_pre[pst], NULL, NULL};
    int rk = _pre[pst], pos = ist, lsize;
    while (_in[pos] != rk) pos++;
    lsize = pos - ist;
    Node *root = new Node;
    root->key = rk;
    root->lchild = createTree(pst + 1, pst + lsize, ist, pos - 1);
    root->rchild = createTree(pst + lsize + 1, ped, pos + 1, ied);
    return root;
}

vector<int> path;

Node *search_path(Node *root, int xx) {
    if (root == NULL) return NULL;
    if (root->key == xx) {
        path.emplace_back(xx);
        return root;
    }
    path.emplace_back(root->key);
    if (xx < root->key) {
        return search_path(root->lchild, xx);
    } else {
        return search_path(root->rchild, xx);
    }
}

int main() {
    scanf("%d%d", &mm, &nn);
    int v, uu, vv;
    for (int i = 0; i < nn; ++i) {
        scanf("%d", &_pre[i]);
        _in[i] = _pre[i];
    }
    sort(_in, _in + nn);
    Node *root = createTree(0, nn - 1, 0, nn - 1);
    for (int i = 0; i < mm; ++i) {
        scanf("%d%d", &uu, &vv);
        Node *U_pointer = search_path(root, uu);
        vector<int> pu = path;
        path.clear();
        Node *V_pointer = search_path(root, vv);
        vector<int> pv = path;
        path.clear();
        if (!U_pointer || !V_pointer) {
            printf("ERROR: ");
            if (!U_pointer && !V_pointer)
                printf("%d and %d are not found.\n", uu, vv);
            else printf("%d is not found.\n", (U_pointer ? vv : uu));
            continue;
        }
        int ii = 0, _min = min(pu.size(), pv.size());
        while (ii < _min) {
            if (pu[ii] != pv[ii]) break;
            ii++;
        }
        int aa = pu[ii - 1];
        if (aa != uu && aa != vv) {
            printf("LCA of %d and %d is %d.\n", uu, vv, aa);
        } else if (uu == aa)
            printf("%d is an ancestor of %d.\n", uu, vv);
        else printf("%d is an ancestor of %d.\n", vv, uu);
    }
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,064评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,606评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,011评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,550评论 1 269
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,465评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,919评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,428评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,075评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,208评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,185评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,191评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,914评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,482评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,585评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,825评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,194评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,703评论 2 339

推荐阅读更多精彩内容