跳到正文

个人记录

2026.3.8常规班课堂总结+题解

Hych2025xx约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年3月8日
收录
2026年3月15日
更新
2026年11月30日
篇幅
约 10 分钟
获赞
1

版本

  1. v3当前
  2. v2
  3. v1

T738339 【pty2021】甜甜唱歌

题意分析

在s中找一个位置,和t尽可能一样,并且不能有连续k个不一样的字符。

60分写法

k = 1,必须和t完全一样。

查找子串。

1.字符串函数s.find(t);查找字符串t在字符串s中第一次出现的位置,如果不存在会是-1(不能直接输出,因为类型是无符号整型unsigned long long,要用一个int变量存起来才能输出)。

C++
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int k;
string s, t;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> k >> s >> t;
	int n = s.size();
	s = "@" + s;
	int pos = s.find(t);
	if(pos != -1){
		cout << pos << '\n';
		return 0;
	}
	return 0;
} 

2.查找子串代码原理,枚举s每个可能的下标,一次去和t进行对比就好了。

C++
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int k;
string s, t;
int f(string s, string t, int x){
	int len = t.size();
	for(int i = 0; i <= len - 1; i++){
		if(t[i] != s[i + x]) return 0;
	}
	return 1;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
    cin >> k >> s >> t;
    int len1 = s.size();
    int len2 = t.size();
    for(int i = 0; i <= len1 - len2; i++){
    	if(f(s, t, i) == 1){
    		cout << i + 1 << '\n';
    		break;
		}
	}
	return 0;
} 

100分写法

如何处理不超过k个连续不同字符呢?

1.维护 cnt,字符不同就加1,字符相同就归零。

2.记录最大相同字符个数的位置,查找子串中多记录一个信息,相同的数量,再找最大值即可。

C++
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int k; 
string s, t; 
int f(string s, string t, int x){
	int len = t.size();
	int cnt = 0, sum = 0;  
	for(int i = 0; i <= len - 1; i++){
		if(t[i] != s[i + x]) cnt++;
		else cnt = 0, sum++; 
		if(cnt == k) return 0;
	}
	return sum;
}
signed main() {
	ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
	cin >> k;
	cin >> s >> t;
	int len1 = s.size();
	int len2 = t.size();
	int maxn = 0, idx; 
	for(int i = 0; i <= len1 - len2; i++){
		int q = f(s, t, i); 
		if(q > maxn){
			maxn = q;
			idx = i + 1;
		}
	}
	cout << idx << '\n';
	return 0;
}

总结

80分代码,竟然眼瞎没有看到条件。读题的时候只是直接带过(一过20分),和读题的习惯有很大的关系,因为只关注重点,而不去看那些很重要的数据范围、特定要求、特殊性质等。

P7072 [CSP-J 2020] 直播获奖

题意分析

一共n秒,每一秒都会给出一个人的成绩,到第i秒的时候有i个人的成绩出来了。

获奖人数:max(1, i * w / 100);(w为获奖百分比)。

输出最后一个获奖的人的分数。

暴力思路

每次直接对前i个人从大到小排序,输出第max(1, i * w / 100)个人的分数。

会因为sort函数时间复杂度,超时。

C++
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 1005;
int n, w, x, sum, ans, p[N];
bool cmp(int x, int y){
    return x > y;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> w;  
    for(int i = 1; i <= n; i++){
        cin >> p[i];
        sort(p + 1, p + n + 1, cmp);
        int hl = max(1, i * w / 100);
        cout << p[hl] << " "; 
    }
	return 0;
} 

100分思路

1.用桶数据数量比较小的时候,统计数量很快。

2.从大到小统计此时的人数,什么时候人数达到x。

C++
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 1005;
int n, w, x, sum, ans, t[N];
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> w;  
    for(int i = 1; i <= n; i++){
        cin >> x;
        t[x]++;
        //找第x个人的分数
        sum = max(1, i * w / 100);//从高到低的人数
        ans = 0;
        for(int j = 600; j >= 0; j--){
            if(t[j] > 0) ans += t[j];
            if(ans >= sum){
                cout << j << " ";
                break;
            }
        }
    }
	return 0;
} 

总结

拿到了暴力50分(老师说是65分,卡数据能拿70分,但是我测了好几遍也没有测出来),这题错的很不应该,好像是做过的题(确实是),但是回忆的时候大脑一片空白,竟然没有想到桶排序。

T738336 【pty2021】甜甜买零食

题意分析

给你n个数字,问有多少种方案,选3个数字总和等于m。

暴力思路

三层循环枚举三个数字,判断相加是否等于m就好了。

C++
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 1e5 + 5;
struct node{
	int sl, id;
}t[N];
int cnt, n, m, p[N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> p[i];
    for(int i = 1; i <= n; i++){
    	for(int j = i + 1; j <= n; j++){
    		for(int k = j + 1; k <= n; k++){
                if(p[i] + p[j] + p[k] == m) cnt++;  
            }
		}
	}
	cout << cnt << '\n';
	return 0;
} 

80分思路

1.优化循环层级,利用条件a[i] + a[j] + a[k] = m,直接算出第三个数字。

2.需求变成了统计数字k在数组中出现的次数。

3.为了避免重复,保证a[i] <= a[j] <= a[k],排序。

4.要不重复计算,统计j + 1到n范围内的k的数量,用二分来查找。

二分找元素第一个大于等于的位置lower_bound(a + 开始位置, a + 结束位置, 查找元素) - a;

二分找第一个大于的位置:upper_bound(a + 开始位置,a + 结束位置, 查找元素) - a;

C++
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 1e5 + 5, M = 1e7 + 5;
int n, m, p[N], sum;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> p[i];
    sort(p + 1, p + n + 1); 
    for(int i = 1; i <= n; i++){
    	for(int j = i + 1; j <= n; j++){
    		int k = m - p[i] - p[j];
    		if(k < p[j]) continue;
    		int l = lower_bound(p + j + 1, p + n + 1, k) - p;
    		int r = upper_bound(p + j + 1, p + n + 1, k) - p - 1;
    		sum += r - l + 1;
		}
	}
	cout << sum << '\n';
	return 0;
} 

总结

拿到了60分基础暴力分,想优化成两层循环,最开始用的是桶,但是无法满足i < j < k,然后用了结构体,记录了数值、位置,还包含了一点桶的性质,但是由于是结构体,访问的时候数组越界了,就一直没有想到二分这个方法,但是老师在讲题的时候(只讲了一点的时候),我突然就想到了二分,但为时已晚。以后可能要多练一下这种算法很普通但是很难想到的题。

原文