【25acm冬令营】专题一

施工中……zzz

easy

Long Loong

(https://vjudge.net/problem/AtCoder-abc336_a)

直接过

题解

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>

using namespace std;

int main()
{
	int N;
	cin >> N;

	cout << 'L';
	for (int i = 0; i < N; i++)
		cout << 'o';
	cout << "ng" << endl;

	return 0;
}

YES or YES?

(https://vjudge.net/problem/CodeForces-1703A)

忽略大小写比较字符串,string.h下有个stricmp()函数,即忽略大小写的strcmp()版本,简单快捷直接拿来用。

题解

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string.h>

using namespace std;

const string YES = "YES";

int main()
{
	int n;
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		string str;
		cin >> str;

		if (!_stricmp(str.c_str(), YES.c_str()))
			cout << "YES";
		else
			cout << "NO";
		cout << endl;
	}

	return 0;
}

Even? Odd? G

(https://vjudge.net/problem/洛谷-P2955)

判断奇偶性。一个int装不下怎么办?用string装,再拿末尾那个数字判断奇偶就行。

题解

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string.h>

using namespace std;

int main()
{
	int N;
	cin >> N;

	for (int i = 0; i < N; i++)
	{
		string str;
		cin >> str;

		int last = str[str.length() - 1];
		if ((last & 1) == 0)
			cout << "even";
		else
			cout << "odd";
		cout << endl;
	}

	return 0;
}

medium

Problem Generator

(https://vjudge.net/problem/CodeForces-1980A)

用哈希表记录每种难度已有的次数即可。

题解

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string.h>

using namespace std;

int htable[7];

int main()
{
	int N;
	cin >> N;

	for (int i = 0; i < N; i++)
	{
        // 重置哈希表
		for (int j = 0; j < 7; j++)
			htable[j] = 0;

		int n, m;
		cin >> n >> m;

		for (int j = 0; j < n; j++)
		{
			char problem;
			cin >> problem;

			htable[problem - 'A']++;
		}

		int count = 0;
		for (int j = 0; j < 7; j++)
		{
			if (htable[j] < m)
				count += (m - htable[j]);
		}

		cout << count << endl;
	}

	return 0;
}

rules

(https://vjudge.net/problem/洛谷-B4012)

一道模拟题,照着题干一步步做就行。

题解

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string.h>

using namespace std;

int main()
{
	int citizens, days, rule;
	cin >> citizens >> days >> rule;

	int goodDays = 0;
	for (int i = 0; i < days; i++)
	{
		int goodCitizens = 0;
		for (int j = 0; j < citizens; j++)
		{
			int curRule;
			cin >> curRule;

			if (curRule == rule)
				goodCitizens++;
		}

		if (goodCitizens >= citizens / 2.0)
			goodDays++;
	}

	if (goodDays >= days / 2.0)
		cout << "YES";
	else
		cout << "NO";

	return 0;
}

hard

Many Replacement

(https://vjudge.net/problem/AtCoder-abc342_c)


题解

(部分正确,O(N*Q)

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string.h>

using namespace std;

int main()
{
	int N;
	cin >> N;
	string S;
	cin >> S;
	int Q;
	cin >> Q;

	for (int i = 0; i < Q; i++)
	{
		char before, after;
		cin >> before >> after;

		if (before == after)
			continue;

		int pos = 0;
		while ((pos = S.find(before, pos)) != string::npos)
		{
			S.replace(pos, 1, 1, after);
		}
	}

	cout << S << endl;
	return 0;
}

(100%正确,O(N+Q))

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string.h>

using namespace std;

char map[128];

int main()
{
	int N;
	cin >> N;
	string S;
	cin >> S;
	int Q;
	cin >> Q;

	// 初始化一个记录字母变化的表
	for (int ch = 'a'; ch <= 'z'; ch++)
		map[ch] = ch;

	for (int i = 0; i < Q; i++)
	{
		char before, after;
		cin >> before >> after;

		if (before == after)
			continue;

		// 更新字母变化表
		for (int ch = 'a'; ch <= 'z'; ch++)
		{
			if (map[ch] == before)
				map[ch] = after;
		}
	}

	for (int i = 0; i < N; i++)
		cout << map[S[i]];
	return 0;
}

更好的交换

(https://vjudge.net/problem/洛谷-B3977)

题解

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string.h>

using namespace std;

int matrix[1005][1005];
int row[1005];
int col[1005];

int main()
{
	int n, m;
	cin >> n >> m;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> matrix[i][j];

	for (int i = 1; i <= n; i++)
		row[i] = col[i] = i;

	for (int i = 0; i < m; i++)
	{
		int code, op1, op2;
		cin >> code >> op1 >> op2;

		if (op1 == op2)
			continue;

		if (code == 1)
			swap(row[op1], row[op2]);
		else
			swap(col[op1], col[op2]);
	}

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			cout << matrix[row[i]][col[j]] << " ";
		}
		cout << endl;
	}

	return 0;
}

小结

  • 忽略大小写比较字符串:stricmp(),位于string.h
  • Hard两道题都是只看结果不看过程的变换题。
posted @ 2025-01-23 22:41  _vertigo  阅读(3)  评论(0编辑  收藏  举报