AcWing第14场周赛题解

A. 3821. 区间选数

题目链接:https://www.acwing.com/problem/content/3824/

题目大意:在两个区间选两个不相同的数。

解题思路:\(l_1, l_2\) ,不行就 \(l_1,l_2 + 1\)

示例程序:

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

int T, l1, r1, l2, r2;

int main() {
    cin >> T;
    while (T--) {
        cin >> l1 >> r1 >> l2 >> r2;
        if (l1 == l2)
            cout << l1 << " " << l2+1 << endl;
        else
            cout << l1 << " " << l2 << endl;
    }
    return 0;
}

B. 3822. 食堂排队

题目链接:https://www.acwing.com/problem/content/3825/

题目大意:模拟打饭,问每位同学打饭的时间。

解题思路:模拟一下。开一个时间戳 t。

示例程序:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int T, n, l[maxn], r[maxn], res[maxn];

int main() {
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i = 0; i < n; i++)
            cin >> l[i] >> r[i];
        int t = 0;
        for (int i = 0; i < n; i++) {
            if (t <= r[i]) res[i] = max(t, l[i]), t = max(t, l[i]) + 1;
            else res[i] = 0;
        }
        for (int i = 0; i < n; i++) {
            if (i) cout << " ";
            cout << res[i];
        }
        cout << endl;
    }
    return 0;
}

C. 3823. 寻找字符串

题目链接:https://www.acwing.com/problem/content/3826/

题目大意:给定字符串 \(s\),求满足如下所有要求的字符串 \(t\)

  1. 字符串 t 是字符串 s 的前缀。
  2. 字符串 t 是字符串 s 的后缀。
  3. 字符串 t 在字符串 s 的中间出现过。也就是作为一个既非前缀也非后缀的子串出现过。
  4. 字符串 t 的长度应尽可能长。

解题思路:KMP模板题。

本题需要你了解 next 数组的含义。

  • 前缀等于后缀 \(\Rightarrow\) 失败指针(即next值)不为空;
  • 中间出现过 \(\Rightarrow\) 指向的位置不是前缀。

然后再整理整理,应该没啥太大问题。

示例程序:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
char s[maxn];
int T, n, nxt[maxn];
bool exist[maxn];

void getnxt() {
    nxt[0] = -1;
    for (int i = 1, j = -1; i < n; i++) {
        while (j != -1 && s[j+1] != s[i]) j = nxt[j];
        if (s[j+1] == s[i]) j++;
        nxt[i] = j;
    }
}

int cal() {
    if (nxt[n-1] == -1) return -1;
    for (int i = 0; i <= nxt[n-1]; i++)
        exist[i] = false;
    for (int i = 1; i+1 < n; i++)
        if (nxt[i] != -1)
            exist[nxt[i]] = true;
    for (int m = nxt[n-1]; m != -1; m = nxt[m]) {
        if (exist[m]) return m+1;
    }
    return -1;
}

int main() {
    scanf("%d", &T);
    while (T--) {
        scanf("%s", s);
        n = strlen(s);
        getnxt();
        int m = cal();
        if (m == -1) puts("not exist");
        else {
            s[m] = 0;
            puts(s);
        }
    }
    return 0;
}
posted @ 2022-04-11 09:44  quanjun  阅读(27)  评论(0)    收藏  举报