Codeforces Round #385 (Div. 2)

本文解析了三道经典的编程题目,包括字符串循环串计数、矩阵形状检测以及最大边数计算等问题,通过代码示例详细介绍了每道题目的解决思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A

题意:统计一个字符串的所有循环串的不同个数

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <utility>
using namespace std;

typedef long long ll;
const int qq = 1e5 + 10;
ll num[qq];

int main(){
	string str;
	map<string , int >  Q;
	cin >> str;
	int c = 1;
	Q[str]++;
	string x;
	for(int i = 1; i < str.size(); ++i){
		for(int j = i; j < str.size(); ++j)
			x += str[j];
		for(int j = 0; j < i; ++j)
			x += str[j];
		if(Q.find(x) == Q.end())	c++;
		Q[x]++;
		x = "";
	}
	cout << c << endl;
	return 0;
}


B

题意:所给图中的所有x都是连在一起的, 问这些x是不是构成了矩形

思路:找出x左上角和右下角的坐标 然后判断是不是矩阵内全是x矩阵外没有x

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <utility>
using namespace std;

typedef long long ll;
const int qq = 550 + 10;
ll num[qq];
char str[qq][qq];

struct Point{
	int x, y;
}agree1, agree2;

int main(){
	int n, m; cin >> n >> m;
	for(int i = 0; i < n; ++i)
		cin >> str[i];
	bool f1, f2;
	f1 = f2 = false;
	for(int i = 0; i < n; ++i)
		for(int j = 0; j < m; ++j)
			if(str[i][j] == 'X'){
				if(!f1)	agree1.x = i, agree1.y = j, f1 = true;
				else{
					if(i < agree1.x){
						agree1.x = i, agree1.y = j;
					}else if(j < agree1.y){
						agree1.x = i, agree1.y = j;
					}
				}
				if(!f2)	agree2.x = i, agree2.y = j, f2 = true;
				else{
					if(i > agree1.x){
						agree2.x = i, agree2.y = j;
					}else if(j > agree1.y){
						agree2.x = i, agree2.y = j;
					}
				}
			}
	bool flag = true;
	for(int i = 0; i < n; ++i)
		for(int j = 0; j < m; ++j){
			if(i <= agree2.x && i >= agree1.x && j <= agree2.y && j >= agree1.y){
				if(str[i][j] == '.')	flag = false;
			}else{
				if(str[i][j] == 'X')	flag = false;
			}
		}
	if(flag)	cout << "YES" << endl;
	else		cout << "NO" << endl;
	return 0;
}

C

题意: n个城市, 有k个首都, k个首都之间都是互不相连的, 图中有m条边, 问最多还能添加多少条边

思路: 因为首都之间都互不相连, 那么每一个首都都一个联通块, 那么我们取k个首都中最多点的联通块把所有没有连上首都的点都连上去再加上其他首都联通块直接能加的边的综合就是答案

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <utility>
using namespace std;

typedef long long ll;
const int qq = 1e3 + 10;
int pre[qq];
int vis[qq];
int node[qq];
int map[qq][qq];
ll num[qq];
ll n, m, k;
ll ver, edges;

int find(int x){
	return pre[x] == x?x:pre[x]=find(pre[x]);
}


vector<int> v[qq];

void dfs(int rt){
	int nodes = v[rt].size();
	for(int i = 0; i < nodes; ++i)
		if(!vis[v[rt][i]]){
			ver++;
			vis[v[rt][i]] = 1;
			edges += v[v[rt][i]].size();
			dfs(v[rt][i]);
		}
}

int main(){
	cin >> n >> m >> k;
	memset(vis, 0, sizeof(vis));
	for(int i = 0; i < k; ++i){
		cin >> node[i];
	}
	int a, b;
	for(int i = 0; i < m; ++i){
		cin >> a >> b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	if(k == 1){
        cout << n*(n-1)/2-m << endl;
        return 0;
	}
	ll sum = 0;
	ll maxn = 1;
	for(int i = 0; i < k; ++i){
		ver = 1;
        edges = 0;
		edges += v[node[i]].size();
		vis[node[i]] = 1;
		if(v[node[i]].size() == 0)	continue;
		dfs(node[i]);
		edges = edges/2;
		maxn = max(maxn, ver);
		sum += ver*(ver-1)/2 - edges;
	}
	int p = 0;
	for(int i = 1; i <= n; ++i){
		if(vis[i])	continue;
		if(v[i].size() == 0)	continue;
		ver = 1;
		edges = 0;
		edges += v[i].size();
		vis[i] = 1;
		dfs(i);
		edges = edges/2;
		sum += ver*(ver-1)/2 -edges;
		num[p++] = ver;
	}
	for(int i = 0; i < p; ++i){
        sum += maxn*num[i];
        maxn += num[i];
	}
	int emptypoint = 0;
	for(int i = 1; i <= n; ++i){
		if(vis[i])	continue;
		if(v[i].size() == 0)	emptypoint++;
	}
	for(int i = 1; i <= emptypoint; ++i){
        sum += maxn;
        maxn++;
	}
	cout << sum << endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值