CSP第33次认证题解

【词频统计】

cpp 复制代码
#include<iostream>
#include<vector>
using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	
	vector<int> x(n+1, 0), y(n+1, 0);
	for(int i = 0; i < n; i++) {
		int l;
		cin >> l;
		vector<bool> f(n+1, true);
		for(int j = 0; j < l; j++) {
			int num;
			cin >> num;
			y[num]++;
			if(f[num]) {
				x[num]++;
				f[num] = false;
			}
		}
	}
	
	for(int i = 1; i <= m; i++) {
		cout << x[i] << " " << y[i] << endl;
	}
	return 0;
}

【相似度计算】

cpp 复制代码
#include<iostream>
#include<string>
#include<map>
using namespace std;

void toLower(string &s) {
	for(char &c : s) {
		c = tolower(c);
	}
}
int main() {
	int n, m;
	cin >> n >> m;
	
	int ans1 = 0, ans2 = 0;
	map<string, int> s1;
	map<string, int> s2;
	map<string, int> visited;
	while(n--) {
		string word;
		cin >> word;
		toLower(word);
		s1[word] = 1;
	}
	while(m--) {
		string word;
		cin >> word;
		toLower(word);
		s2[word] = 1;
		if(s1.count(word)&&!visited.count(word)) {
			ans1++;
			visited[word] = 1;
		}
	}
	
	ans2 = s1.size()+s2.size()-ans1;
	
	cout << ans1 << endl;
	cout << ans2 << endl;
	return 0;
}

【化学方程式配平】

cpp 复制代码
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<cmath>
using namespace std;

void solve() {
	int m;
	cin >> m;
	
	vector<vector<float>> A(40, vector<float>(m, 0));
	map<string, int> visited;
	
	int rank = 0;
	for(int i = 0; i < m; i++) {
		string str;
		cin >> str;
		string eltmt = "";
		string key;
		float index = 0;
		bool lastCh = true;
		for(char c : str) {
			if(c <= 'z' && c >= 'a') {
				if(!lastCh) {
					lastCh = true;
					if(visited.count(key)) {
						A[visited[key]][i] = index;
					} else {
						A[rank][i] = index;
						visited[key] = rank;
						rank++;
					}
					index = 0;
				}
				eltmt += c;
			} else {
				if(lastCh) {
					lastCh = false;
					key = eltmt;
					eltmt = "";
				}
				index = index*10 + (c-'0');
			}
		}
		if(visited.count(key)) {
			A[visited[key]][i] = index;
		} else {
			A[rank][i] = index;
			visited[key] = rank;
			rank++;
		}
	}
	
	int startI = 0;
	int startJ = 0;
	int k = m;
	while(k--) {
		bool zeroJ = true;
		for(int i = startI; i < rank; i++) {
			if(A[i][startJ] != 0) zeroJ = false;
		}
		if(zeroJ) {
			startJ++;
		} else {
			if(A[startI][startJ] == 0) {
				int r;
				for(int i = startI; i < rank; i++) {
					if(A[i][startJ] != 0) r = i;
				}
				for(int j = startJ; j < m; j++) {
					swap(A[startI][j], A[r][j]);
				}
			}
			for(int i = startI+1; i < rank; i++) {
				float multi = A[i][startJ]/A[startI][startJ];
				for(int j = startJ; j < m; j++) {
					A[i][j] -= A[startI][j]*multi;
				}
			}
			startI++;
			startJ++;
		}
	}
	
	for(int i = 0; i < rank; i++) {
		for(int j = 0; j < m; j++) {
			if(fabs(A[i][j])< 0.1) A[i][j] = 0;
		}
	}
	int rk = rank;
	for(int i = rank-1; i >= 0; i--) {
		bool f = true;
		for(int j = 0; j < m; j++) {
			if(A[i][j] != 0) {
				f = false;
			}
		}
		if(f) rk--;
	}
	
	
	if(rk < m) cout << "Y" << endl;
	else   cout << "N" << endl;
}
int main() {
	int n;
	cin >> n;
	
	while(n--) {
		solve();
	}
	
	return 0;
}

【感慨】

上面的题目都是自己独立做出来的,真的很有成就感,明显感觉到自己代码能力的提升,谁懂啊,真的开心极了😊

相关推荐
华科大胡子3 小时前
开源项目Git贡献
c++
比昨天多敲两行3 小时前
C++ 继承
开发语言·c++·面试
2501_908329853 小时前
C++中的装饰器模式实战
开发语言·c++·算法
程序猿编码3 小时前
Linux 进程注入:从调试器到武器化的技术演进
linux·运维·服务器·c++·进程注入
Rabitebla3 小时前
[特殊字符] TopK问题全解析(TomGo复习版|讲人话 + 原理打穿)
c语言·数据结构·算法·链表
我是玄兔3 小时前
2022年复试题
数据结构·算法
keep intensify3 小时前
最小路径和
算法·leetcode·职场和发展
袋鼠云数栈3 小时前
黄仁勋 GTC 2026 之后,为何AI 时代的数据底座正在被重新定义?
大数据·数据结构·人工智能·架构·多模态
swipe3 小时前
做 RAG 不能只会检索:为什么 Loader 和 Splitter 才是知识库入库的第一步
算法·llm·agent