2022暑期集训测试1(几道有趣的题)

· · 个人记录

(是第一期哦)

Problem 1

题目链接:https://www.mfstem.org/p/1340

Background

Special for beginners, ^_^

Description

给定一个字符串 s,请你找出其中不含有重复字符的最长子串的长度。

Format Input

一行,一个字符串 s,长度在 0∼50000 之间,由英文字母、数字和空格组成。

Output

输出一个整数,为不含有重复字符的最长子串的长度。

Samples

输入数据 1

abcabcbb

输出数据 1

3

Hint1

因为无重复字符的最长子串是 "abc",所以其长度为 3。

输入数据 2

bbbbb

输出数据 2

1

Hint2

因为无重复字符的最长子串是 "b",所以其长度为 1。

输入数据 3

pwwkew

输出数据 3

3

Hint3

因为无重复字符的最长子串是 "wke",所以其长度为 3。

请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

Limitation

1s, 1024KiB for each test case.

解:

看到这道题之后,我的第一反应就是,这不是很简单嘛,以i为初始点,j为结束点,j从i至最后一个字母,判断重复不得了?写出了如下代码

#include <bits/stdc++.h>
using namespace std;
const int N = 50008;
char str[N];
bool book[1<<7];
int ans;
int main() {
    scanf("%s", str+1);
    int n = strlen(str+1);
    for (int i = 1; i <= n; i++) {
        int cnt = 0;
        memset(book, false, sizeof book);
        for (int j = i; j <= n; j++) {
            if (book[str[j]])
                break;
            book[str[j]] = true;
            cnt++;
        }
        ans = max(ans, cnt);
    }
    printf("%d", ans);
    return 0;
}

结果一提交:

X 30 Wrong Answer
# 状态 耗时 内存占用
#1 Accepted 2ms 404 KiB
#2 Accepted 2ms 404 KiB
#3 Wrong Answer 2ms 384 KiB
#4 Accepted 2ms 324 KiB
#5 Wrong Answer 1ms 376 KiB
#6 Wrong Answer 2ms 376 KiB
#7 Wrong Answer 2ms 376 KiB
#8 Wrong Answer 2ms 376 KiB
#9 Wrong Answer 2ms 376 KiB
#10 Wrong Answer 2ms 376 KiB

好哇,场面是多么的惨烈壮观哪。

请大家注意题目中的这两个字(相信细心的朋友们早就注意到了):

一行,一个字符串 s,长度在 0∼50000 之间,由英文字母、数字和空格(???)组成。

嘿,你个老六

于是,我规规矩矩地把

scanf("%s", str+1);

改成了

gets(str+1);

代码变成了这样:

#include <bits/stdc++.h>
using namespace std;
const int N = 50008;
char str[N];
bool book[1<<7];
int ans;
int main() {
    gets(str+1);
    int n = strlen(str+1);
    for (int i = 1; i <= n; i++) {
        int cnt = 0;
        memset(book, false, sizeof book);
        for (int j = i; j <= n; j++) {
            if (book[str[j]])
                break;
            book[str[j]] = true;
            cnt++;
        }
        ans = max(ans, cnt);
    }
    printf("%d", ans);
    return 0;
}

结果变成了

0 Compile Error

编译错误?????

怎么回事呢?

foo.cc: In function 'int main()':
foo.cc:8:2: error: 'gets' was not declared in this scope; did you mean 'fgets'?
    8 |  gets(str+1);
      |  ^~~~
      |  fgets
foo.cc:14:18: warning: array subscript has type 'char' [-Wchar-subscripts]
   14 |    if (book[str[j]])
      |             ~~~~~^
foo.cc:16:14: warning: array subscript has type 'char' [-Wchar-subscripts]
   16 |    book[str[j]] = true;
      |         ~~~~~^

gets()函数没有定义?怎么回事?我到网上搜了一下——

原来,Windows下的gets函数读取到的换行符是\r\n,而Linux/UNIX下读取到的换行符仅\n。而未经过处理的Windows下代码直接放到Linux平台下,当然会发生错误。所以,不建议大家在整行读取时用gets,用getline可能会更好些:

#include <bits/stdc++.h>
using namespace std;
const int N = 50008;
string str;
bool book[1<<7];
int ans;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    getline(cin, str);
    int n = str.length();
    for (int i = 0; i < n; i++) {
        int cnt = 0;
        memset(book, false, sizeof book);
        for (int j = i; j < n; j++) {
            if (book[str[j]])
                break;
            book[str[j]] = true;
            cnt++;
        }
        ans = max(ans, cnt);
    }
    printf("%d", ans);
    return 0;
}

这样就过了。

老师给的标准答案:

#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 7;
int pos[N];
int main() {
    int ans = 0;
    vector<int> ab(128, -1);
    string s;
    getline(cin, s);
    for (int i = 0; i < s.size(); ++i)
        pos[i] = ab[s[i]], ab[s[i]] = i;
    for (int i = 0, j = 0; j < s.size(); ++j) {
        if (pos[j] >= i) i = pos[j] + 1;
        ans = max(ans, j-i+1);
    }
    cout << ans;
    return 0;
}

看上去是不是简短很多?

思路是这样的: 还是以i为起点,以j为终点,长度还是j-i+1。只不过增加了一个数组pos。pos[j]指的是在字符串s中,字符s[j]上次出现的位置,这种方式在平时也是很常见的。这个if语句的意思就是:如果字符s[j]上一次出现的位置大于等于i,也就说明字符s[j]重复出现了,那么就需要将起始位置i置于这个字符上次出现的后面一个位置,也就能避免重复。

Problem 2

题目链接:https://www.mfstem.org/p/1341

Background

Special for beginners, ^_^

Description

上大学了,小J学会了一种加密算法,这种算法可以根据一个行数 m,将一个字符串进行重新排列,变成无法读懂的密文。

例如:给定一个字符串“PAYPALISHIRING”,行数3,将其进行从上到下,从左到右的Z字形排列如下:

P   A   H   N
A P L S I I G
Y   I   R

之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:“PAHNAPLSIIGYIR”。

Format

Input

一个由英文字母及英文标点符号组成的字符串 s 及一个行数 m(1≤m≤1000),s 的长度在 1∼1000 之间。

Output

输出加密字符串

Samples

输入数据 1

PAYPALISHIRING 3

输出数据 1

PAHNAPLSIIGYIR

输入数据 2

PAYPALISHIRING 4

输出数据 2

PINALSIGYAHRPI

Hint2

P     I    N
A   L S  I G
Y A   H R
P     I

输入数据 3

A 1

输出数据 3

A

输入数据 4

hello,world! 3

输出数据 4

horel,ol!lwd

Limitation

1s, 1024KiB for each test case.

解:

这一题相对于上面那题来说,还是十分简单的,坑也比较少,暴力模拟就行。

记住,暴力出奇迹!!!!!

不过,这个“z”字形是有一个周期的,长度为2m+2。当周期控制变量j小于m时往下(x++),否则往右上(x--, y++)。我的代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1008;
char s[N];
int isc[N][N], n, m, y = 1, cnt;
char ans[N][N];
int main() {
    scanf("%s%d", s+1, &m);
    if (m == 1) {
        printf("%c", s[1]);
        return 0;
    }
    n = strlen(s+1);
    for (int i = 1, j = 1, x = 1; i <= n; i++, j = j%(2*m-2)+1) {
        ans[x][y] = s[i];
        isc[x][y] = true;
        if (j < m)
            x++;
        else
            x--, y++;
    }
    for (int i = 1; i<=m && cnt!=n; i++)
        for (int j = 1; j <= y; j++) {
            if (isc[i][j]) {
                cnt++;
                printf("%c", ans[i][j]);
            }
            if (cnt == n)
                break;
        }
    return 0;
}

老师的示范代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1007;
char m[N][N];
int main() {
    int col = 0, c;
    string s;
    cin >> s >> c;
    if (c == 1) {
        cout << s;
        return 0;
    }
    for (int i = 0, r = 0; i < s.size(); ++i) {
        if (col%(c-1) == 0) {
            m[r++][col] = s[i];
            if (r == c) r = c - 2, ++col;
        } else {
            m[r--][col++] = s[i];
        }
    }
    for (int i = 0; i < c; ++i)
        for (int j = 0; j <= col; ++j)
            if (m[i][j]) putchar(m[i] [j]);
    return 0;
}

Problem 3

题目链接:https://www.mfstem.org/p/1342

Background

Special for beginners, ^_^

Description

给出两个整数 n,m。问有多少个长度为 n 的序列 A,满足以下条件:

• 1 ≤ A[i] ≤ m(i = 1,2,…,n)

• ∀i∈[1,n-1],A[i+1]是A[i]的倍数。

由于答案可能很大,所以你只需要输出答案对998244353取模的结果。

Format

Input

输入只有一行,两个整数n,m(1≤n,m≤2×10^5)。

Output

输出只有一行,输出方案数。

Samples

输入数据 1

3 4

输出数据 1

13

输入数据 2

20 30

输出数据 2

71166

输入数据 3

200000 200000

输出数据 3

835917264

Limitation

1s, 1024KiB for each test case.

解:

看到这道题,我的第一反应是懵逼:什么情况,ACM啥时候开始这么为难我们了?

所以,我是一脸的“没有头绪”,那题我也没做。后来思路出来后,我依然感觉有点难。

咱们先来聊聊思路:

首先,确定大致方向。刚刚看到这题时,我的第一反应是dfs。但是,dfs的时间复杂度是O(2^N),而这里N是200000(二十万),所以不行。而正确思路却是从m至1枚举末尾而不是开头,并判断倍数情况。

话不多说,咱们先贴代码:

#include <iostream>
const int M = 998244353;
int dp[20] = {1}, n, m;

int solve(int k) {
    int ans = 1;
    for (int i = 2; i*i <= k; ++i) {
        if (k % i) continue;
        int c = 0;
        while (k%i == 0) k /= i, ++c;
        ans = ans * 1LL * dp[c] % M;
    }
    if (k > 1) ans = ans * 1LL * dp[1] % M;
    return ans;
}
int main() {
    int ans = 0;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= 19; ++j)
            (dp[j] += dp[j-1]) %= M;
    for (int i = 1; i <= m; ++i)
        (ans += solve(i)) %= M;
    printf("%d", ans);
    return 0;
}

看到dp,我猜有不少人都想到了动态规划。没错,这题是一个背包问题。所以,我们先将dp[]增加一个维度,变成dp[][]。那么,dp[i][j]就是在长度为i的情况下使用了≤j个因子的情况下,所有的排列组合数。由于我们只需要dp[n][],所以,理所应当的,我们把它降维。solve函数,自然就是排列组合数了。

Problem 4

题目链接:mfstem.org/p/1343

Background

Special for beginners, ^_^

Descirption

给你一个字符串s和一个字符规律p,请你来实现一个支持.和*的正则表达式匹配。

▪ .匹配任意单个字符

▪ *匹配零个或多个前面的那一个元素

所谓匹配,是要涵盖 整个 字符串s的,而不是部分字符串。

Format

Input

多组测试数据 每组数据一行,两个字符串s(长度小于20)和p(长度小于30),s只包含小写字母,p只包含小写字母以及.和*。

Output

每组数据输出一行,如果p能够匹配s,则输出Yes,否则输出No。

Samples

输入数据 1

aa a
aa aa
aa a.
aa b*aa
aa c*a.
aaaaab a*b

输出数据 1

No
Yes
Yes
Yes
Yes
Yes

Limitation

1s, 1024KiB for each test case.

解:

这一题,我用上了我的老套路:设置两个计数数组,判断字母数量就行。唯一二需要注意的是.和*。.的话,我用了一个dot变量记录点的数量,一会统计时如果剩余无法匹配的数量和点的数量相同的话,那么就能匹配;否则,就不匹配。至于*,就把s的计数数组和p的计数数组都置为0,并给此字符设置标记,一旦扫描到有标记的字母就直接跳过。

#include <bits/stdc++.h>
using namespace std;
#define mems(x) memset(x, 0, sizeof(x))
const int CHARN = (1<<7), N = 1008;
int cnts[CHARN], cntp[CHARN], dot, sx;
char s[N], p[N];
bool book[CHARN];
void init() {
    mems(cnts);
    mems(cntp);
    mems(book);
    dot = 0;
    sx = 0;
}
int main() {
    while (~scanf("%s%s", s+1, p+1)) {
        init();
        int n = strlen(s+1), m = strlen(p+1);
        if (!strcmp(s+1, p+1)) {
            puts("Yes");
            continue;
        }
        for (int i = 1; i <= n; i++)
            cnts[s[i]]++;
        for (int i = 1; i <= m; i++) {
            if (p[i] == '*') {
                cnts[p[i-1]] = cntp[p[i-1]] = 0;
                book[p[i-1]] = true;
                continue;
            }
            if (p[i] == '.') {
                dot++;
                continue;
            }
            if (book[p[i]])
                continue;
            cntp[p[i]]++;
        }
        for (int i = 'a'; i <= 'z'; i++)
            sx += cnts[i]-cntp[i];
        if (sx == dot)
            puts("Yes");
        else
            puts("No");
    }
    return 0;
}

可惜,理想很丰满,现实超骨感:

75 Wrong Answer

怎么回事儿?原来,

for (int i = 'a'; i <= 'z'; i++)
    sx += cnts[i]-cntp[i];

中的第二行代码少了一个abs()函数!!!

修改之后,for循环变成了这样:

for (int i = 'a'; i <= 'z'; i++)
    sx += abs(cnts[i]-cntp[i]);

代码变成了这样:

#include <bits/stdc++.h>
using namespace std;
#define mems(x) memset(x, 0, sizeof(x))
const int CHARN = (1<<7), N = 1008;
int cnts[CHARN], cntp[CHARN], dot, sx;
char s[N], p[N];
bool book[CHARN];
void init() {
    mems(cnts);
    mems(cntp);
    mems(book);
    dot = 0;
    sx = 0;
}
int main() {
    while (~scanf("%s%s", s+1, p+1)) {
        init();
        int n = strlen(s+1), m = strlen(p+1);
        if (!strcmp(s+1, p+1)) {
            puts("Yes");
            continue;
        }
        for (int i = 1; i <= n; i++)
            cnts[s[i]]++;
        for (int i = 1; i <= m; i++) {
            if (p[i] == '*') {
                cnts[p[i-1]] = cntp[p[i-1]] = 0;
                book[p[i-1]] = true;
                continue;
            }
            if (p[i] == '.') {
                dot++;
                continue;
            }
            if (book[p[i]])
                continue;
            cntp[p[i]]++;
        }
        for (int i = 'a'; i <= 'z'; i++)
            sx += abs(cnts[i]-cntp[i]);
        if (sx == dot)
            puts("Yes");
        else
            puts("No");
    }
    return 0;
}

分数变成了这样:

100 Accepted

老师给的代码是这样:

#include <bits/stdc++.h>
using namespace std;
string s, p;
bool dp[50][50];
bool check(int i, int j) {
    if (i == 0) return false;
    if (p[j-1] == '.') return true;
    return s[i-1] == p[j-1];
}
int main() {
    while(cin >> s >> p) {
        int m = s.size();
        int n = p.size();
        memset(dp, false, sizeof dp);
        dp[0][0] = true;
        for (int i = 0; i < m; i++)
            for (int j = 1; j <= n; j++)
                if (p[j-1] == '*') {
                    dp[i][j] |= dp[i][j- 2];
                    if (check(i, j-1)) {
                        dp[i][j] |= dp[i-1][j];
                    }
                } else if (check(i, j))
                    dp[i][j] |= dp[i-1] [j-1];
        if(dp[m][n])
            cout << "Yes";
        else
            cout << "No";
    }
    return 0;
}

老师的大致思路是这样:

我也没想明白,你们自己去琢磨吧

下一期:https://www.luogu.com.cn/blog/matthew101130/post-2022-shu-ji-ji-xun-ce-shi-2-ye-shi-ji-dao-you-qu-di-ti-post

我们下次再见!