首页 > 其他 > 详细

天梯赛练习2 补题

时间:2020-10-18 12:27:16      阅读:0      评论:0      收藏:0      [点我收藏+]

L1-064

字符串模拟。

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

int main() {
    int t;
    cin >> t;
    getchar();
    while(t --) {
        string s;
        getline(cin, s);
        cout << s << endl;
        while(s[0] == ‘ ‘)
            s.erase(s.begin());
        while(s[s.size() - 1] == ‘ ‘)
            s.erase(s.end() - 1);
        for(int i = 0; i < s.size(); ++ i) {
            if(s[i] == ‘ ‘) {
                while(s[i + 1] == ‘ ‘)
                    s.erase(s.begin() + i + 1);
                if(!isalnum(s[i + 1]))
                    s.erase(s.begin() + i);
            }
        }
        for(int i = 0; i < s.size(); ++ i)
            if(s[i] >= ‘A‘ && s[i] <= ‘Z‘ && s[i] != ‘I‘)
                s[i] += (‘a‘ - ‘A‘);
        for(int beg = 0;; ++ beg) {
            beg = s.find("can you", beg);
            if(beg == -1) break;
            if((!beg || !isalnum(s[beg - 1])) &&
               (beg + 7 == s.size() || !isalnum(s[beg + 7])))
                s.replace(beg, 7, "# can");
        }
        for(int beg = 0;; ++ beg) {
            beg = s.find("could you", beg);
            if(beg == -1) break;
            if((!beg || !isalnum(s[beg - 1])) &&
               (beg + 9 == s.size() || !isalnum(s[beg + 9])))
                s.replace(beg, 9, "# could");
        }
        for(int beg = 0;; ++ beg) {
            beg = s.find("I", beg);
            if(beg == -1) break;
            if((!beg || !isalnum(s[beg - 1])) &&
               (beg + 1 == s.size() || !isalnum(s[beg + 1])))
                s.replace(beg, 1, "you");
        }
        for(int beg = 0;; ++ beg) {
            beg = s.find("me", beg);
            if(beg == -1) break;
            if((!beg || !isalnum(s[beg - 1])) &&
               (beg + 2 == s.size() || !isalnum(s[beg + 2])))
                s.replace(beg, 2, "you");
        }
        for(int i = 0; i < s.size(); ++ i) {
            if(s[i] == ‘?‘) s[i] = ‘!‘;
            if(s[i] == ‘#‘) s[i] = ‘I‘;
        }
        cout << "AI: " << s << endl;
    }
    return 0;
}

L2-002

构造链表。

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

const int maxn = 1e5;
struct Node {
    int address, key, next, num;
} node[maxn];
bool vis[maxn];
bool cmp(Node a, Node b) {
    return a.num < b.num;
}
int main() {
    int head, n, a;
    scanf("%d%d", &head, &n);
    int k1 = 0, k2 = 0;
    for (int i = 0; i < maxn; ++ i) {
        node[i].num = 2 * maxn;
    }
    for (int i = 0; i < n; ++ i) {
        scanf("%d", &a);
        scanf("%d%d", &node[a].key, &node[a].next);
        node[a].address = a;
    }
    for (int i = head; i != -1; i = node[i].next) {
        if (!vis[abs(node[i].key)]) {
            vis[abs(node[i].key)] = true;
            node[i].num = k1;
            k1 ++;
        } else {
            node[i].num = maxn + k2;
            k2 ++;
        }
    }
    sort(node, node + maxn, cmp);
    int k = k1 + k2;
    for (int i = 0; i < k; ++ i) {
        if (i != k1 - 1 && i != k - 1) {
            printf("%05d %d %05d\n", node[i].address, node[i].key, node[i + 1].address);
        } else {
            printf("%05d %d -1\n", node[i].address, node[i].key);
        }
    }
    return 0;
}

天梯赛练习2 补题

原文:https://www.cnblogs.com/DariusOrz/p/13834228.html

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号