AK 9.12 百度Java后端研发B卷 笔试

T1(博弈论)

cpp 复制代码
#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int n, m, t;

void solve() {
	cin >> n >> m; t = n + m - 2;
	if(t & 1) cout << "Yes" << endl;
	else cout << "No" << endl;
}

int main() {
	cin.tie(0); cout.tie(0);
	std::ios::sync_with_stdio(false);

	int T = 1;
	cin >> T;
	while(T --) {
		solve();
	}

	return 0;
}

T2(思维,模拟,哈希表,位运算)

cpp 复制代码
#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int n, m;
int a[N];

int xor_in = 0, XOR_M;
unordered_map<int, int> mp;
unordered_map<int, int> in, out;

bool check() {
	if(in.size() == m && xor_in == XOR_M) {
		cout << "YES" << endl;
		cout << 0 << endl;
		return true;
	}

	if(in.size() == m - 1 && out.size() == 1) {
		int target = XOR_M ^ xor_in;
		int idx = mp[target];
		if(idx != 0) {
			cout << "YES" << endl;
			cout << 1 << endl;
			cout << (out.begin())->second << ' ' << idx << endl;
			return true;
		}
	}

	return false;
}

void solve() {
	cin >> n >> m;
	for(int i = 1; i <= n; i ++) cin >> a[i];
	for(int i = 1; i <= m; i ++) XOR_M ^= i;

	for(int i = 1; i <= m; i ++) {
		if(a[i] > m) out[a[i]] = i;
		else in[a[i]] = i, xor_in ^= a[i];
	}

	for(int i = m + 1; i <= n; i ++) mp[a[i]] = i;

	if(check()) return ;

	for(int i = 1, j = m + 1; j <= n; i ++, j ++) {
		if(a[i] >= 1 && a[i] <= m) {
			in.erase(a[i]);
			xor_in ^= a[i];
		} else out.erase(a[i]);

		if(a[j] >= 1 && a[j] <= m) in[a[j]] = j, xor_in ^= a[j];
		else out[a[j]] = j;

		mp.erase(a[j]);
		mp[a[i]] = i;

		if(check()) return ;
	}

	cout << "NO" << endl;
}

int main() {
	cin.tie(0); cout.tie(0);
	std::ios::sync_with_stdio(false);

	int T = 1;
//	cin >> T;
	while(T --) {
		solve();
	}

	return 0;
}

T3(模拟,哈希表)

cpp 复制代码
#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

unordered_map<string, unordered_map<string, unordered_set<string>>> fun; // name : args : type

string get_type(string op) {
	string ret;
	for(char c : op) {
		if(c == ' ') break;
		ret += c;
	}
	return ret;
}

string get_name(string op) {
	string ret;
	int idx = op.find('('); idx -- ;
	for(; idx >= 0; idx --) {
		if(op[idx] == ' ') break;
		ret += op[idx];
	}

	reverse(ret.begin(), ret.end());

	return ret;
}

string get_args(string op) {
	int a = op.find('('), b = op.find(')');
	op = op.substr(a + 1, b - a - 1); op += ',';

	vector<string> ss; string t;

	for(char c : op) {
		if(c == ',') ss.push_back(t), t = "";
		else t += c;
	}

	string ret;
	for(string s : ss) {
		if(s.find(' ') != -1) ret += s.substr(0, s.find(' ')) + ",";
		else ret += s + ",";
	}

	return ret;
}

void solve() {
	int T, code;
	cin >> T;

	string op;
	while(T --) {
		cin >> code;
		getline(cin, op);
		getline(cin, op);
		if(code == 1) {
			string type = get_type(op);
			string name = get_name(op);
			string args = get_args(op);
			if(fun.count(name) == 0) {
				fun[name][args].insert(type);
				cout << "ok." << endl;
			} else {
				if(fun[name].count(args) == 0) {
					fun[name][args].insert(type);
					cout << "ok." << endl;
				} else {
					cout << "method " << name << " is already defined." << endl;
				}
			}
		} else if(code == 2) {
			string type = get_type(op);
			string name = get_name(op);
			string args = get_args(op);

			if(fun.count(name) != 0 && fun[name].count(args) != 0) cout << "ok." << endl;
			else if(fun.count(name) == 0) cout << "cannot find symbol " << name << "." << endl;
			else if(fun.count(name) != 0 && fun[name].count(args) == 0) {
				cout << "method " << name << " cannot be applied to given types." << endl;
			}
		}
	}

}

int main() {
	cin.tie(0); cout.tie(0);
	std::ios::sync_with_stdio(false);

	solve();

	return 0;
}
相关推荐
想不明白的过度思考者几秒前
Java从入门到“放弃”(精通)之旅——JavaSE终篇(异常)
java·开发语言
YuforiaCode15 分钟前
第十二届蓝桥杯 2021 C/C++组 直线
c语言·c++·蓝桥杯
.生产的驴25 分钟前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
猿周LV32 分钟前
JMeter 安装及使用 [软件测试工具]
java·测试工具·jmeter·单元测试·压力测试
知来者逆34 分钟前
计算机视觉——速度与精度的完美结合的实时目标检测算法RF-DETR详解
图像处理·人工智能·深度学习·算法·目标检测·计算机视觉·rf-detr
晨集34 分钟前
Uni-App 多端电子合同开源项目介绍
java·spring boot·uni-app·电子合同
时间之城36 分钟前
笔记:记一次使用EasyExcel重写convertToExcelData方法无法读取@ExcelDictFormat注解的问题(已解决)
java·spring boot·笔记·spring·excel
阿让啊39 分钟前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
এ᭄画画的北北39 分钟前
力扣-160.相交链表
算法·leetcode·链表
椰羊~王小美44 分钟前
LeetCode -- Flora -- edit 2025-04-25
java·开发语言