【第二次CSS CSP】 201409 (C++)

201409-1

 

解题思路:数据量小,直接暴力枚举

参考代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n;
int a[N];
int ans;
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		for (int j = i + 1; j <= n; j++) {
			if (abs(a[i] - a[j]) == 1) ans++;
		}
	}
	cout << ans;
	return 0;
}

201409-2

 

解题思路:直接暴力,连差分都用不上 

参考代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n;
int g[105][105];
int ans;
int main() {
	cin >> n;
	int x1, y1, x2, y2;
	for (int i = 1; i <= n; i++) {
		cin >> x1 >> y1 >> x2 >> y2;
		for (int x = x1; x < x2; x++) {
			for (int y = y1; y < y2; y++) {
				g[x][y] = 1;
			}
		}
	}
	for (int i = 0; i <= 100; i++) {
		for (int j = 0; j <= 100; j++) {
			if (g[i][j]) ans++;
		}
	}
	cout << ans;
	return 0;
}

201409-3

 

解题思路:暴力匹配甚至不需要KMP

参考代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
string s, x, y;
int n, is;
bool check(string y) {
	int len = s.size();
	if (y.size() < len) return 0;
	for (int i = 0; i + len - 1 < y.size(); i++) {
		if (y.substr(i, len) == s) return 1;
	}
	return 0;
}
int main() {
	cin >> s >> is >> n;
	if (is == 0) {
		for (int i = 0; i < s.size(); i++) {
			if (s[i] <= 'Z' && s[i] >= 'A') s[i] -= 'A' - 'a';
		}
	}
	for (int i = 1; i <= n; i++) {
		cin >> x;
		y = x;
		if (is == 0) {
			for (int j = 0; j < y.size(); j++) {
				if (y[j] <= 'Z' && y[j] >= 'A') y[j] -= 'A' - 'a';
			}
		}
		if (check(y)) cout << x << endl;
	}
	return 0;
}

 

201409-4

 

解题思路:把分店的位置都放到队列里,BFS求最短路就行了

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e3 + 5;
int t[N][N];
bool is[N][N];
int n, m, k, d;
struct point {
	int x, y, w;
} g[N * N];
ll ans;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
void init() {
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			t[i][j] = N * N;
		}
	}
}
queue<point>q;
void bfs() {
	while (!q.empty()) {
		point now = q.front();
		q.pop();
		int tw = now.w + 1;
		for (int i = 0; i < 4; i++) {
			int tx = now.x + dx[i];
			int ty = now.y + dy[i];
			if (tx >= 1 && tx <= n && ty >= 1 && ty <= n) {
				if (is[tx][ty] == 0 && tw < t[tx][ty]) {
					t[tx][ty] = tw;
					q.push({tx, ty, tw});
				}
			}
		}
	}
}
int main() {
	cin >> n >> m >> k >> d;
	init();
	int x, y;
	for (int i = 1; i <= m; i++) {
		cin >> x >> y;
		q.push({x, y, 0});
		t[x][y] = 0;
	}
	for (int i = 1; i <= k; i++) {
		cin >> g[i].x >> g[i].y >> g[i].w;
	}
	for (int i = 1; i <= d; i++) {
		cin >> x >> y;
		is[x][y] = 1;
	}
	bfs();
	for (int i = 1; i <= k; i++) {
		ans += g[i].w * t[g[i].x][g[i].y];
	}
	cout << ans;
	return 0;
}

201409-5

解题思路:

参考代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值