Codeforces - 990C

本文解析Codeforces990C题目,介绍如何通过计算每个字符串中未匹配的括号数量来确定可能的匹配组合总数。对于每组输入,算法统计左括号和右括号无法在自身内匹配的数量,并利用这些信息计算总的匹配组合。

<Codeforces 990C>

题意:

给n个只由 '(' 和 ')' 组成的串,问从这些串中选出两个串,使他们括号匹配,能够成多少个匹配串,自身和自身组合匹配也算。以题中第二个样例说明:

输入:2 ( ) ( )

输出:4

解释:输入的两个串 a,b 分别是 a: ( ),b: ( ),所以共四种连接方式,即 a->a,a->b,b->a,b->b

思路:

说实话,这个思路,就是一种只可意会不可言传的感觉,我大概用我拙劣的语言,表述一下 T^T

我先对每个输入进来的串,进行如下操作:

int ln = s.length();
for(int j = 0; j < ln; j++) {
	if(s[j] == '(') l++;
	else {
		if(l) l--;
		else r++;
	}
}

这里的 l 代表,在输入进来的这个串中,不能与自身右括号形成匹配的左括号的个数;

r 则代表,在输入进来的这个串中,不能与自身左括号形成匹配的右括号的个数。

我已经进我最大努力表述了,自行脑补一下这里有一个笑哭的表情。。。注意听~

这样每个串无法在自身形成匹配的左右括号数就分别找出来了。

然后进行如下两步操作:

if(!l) R[r]++;
if(!r) L[l]++;

以上两步是核心代码,我尽我最大努力解释清楚,

首先明确两点,

(1)如果某个串里 l != 0 && r != 0,那这个串没法和任何一个串进行匹配

(2)有且仅有 x 个左括号不能在自身匹配的串, 它要想配对, 必须找有且仅有 x 个右括号不能在自身匹配的串

这也就是L[ ], R[ ]这两个数组的作用,如果 l == 0,那就意味着,这个串里有且仅有 r 个右括号匹配不到 ( r 可以取0),这样,它就必须找一个,有且仅有 r 个左括号匹配不到的的串,进行匹配。所以我在 l == 0 时,进行R[r]++,就是为了找到一个L[r]和当前的R[r]进行相乘再加入ans里,L[ ],R[ ]数组分别代表的值是仅有左括号不匹配,和仅有右括号不匹配的串的个数,而L[i], R[i]中的 i 值,则分别代表不匹配的左右括号各有几个。r == 0 同理。

本人AC代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <ctime>
#include <cctype>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 7;
const int Inf = 1e9 + 7;
pair <int, int> a[maxn];
queue <int> qua;
set <int> sst;
vector <int> vect;
map <int, int> mp;
priority_queue < int, vector<int>, less<int> > qua1;
priority_queue < int, vector<int>, greater<int> > qua2;
int n;
string s;
ll L[maxn], R[maxn]; //有pos个左括号不能在自身匹配的串, 它要想配对, 必须找有pos个右括号不能在自身匹配的串
// 而L[pos]和R[pos]记录的就是左右括号与自身不匹配的串的个数, 即ans += L[pos] * R[pos]
ll ans;

int main() {
	cin >> n;
	int l, r; //记录每个串中左、右括号无法在该串自身内进行匹配的个数分别为多少
	for(int i = 1; i <= n; i++) {
		cin >> s;
		l = r = 0;
		int ln = s.length();
		for(int j = 0; j < ln; j++) {
			if(s[j] == '(') l++;
			else {
				if(l) l--;
				else r++;
			}
		}
		if(!l) R[r]++;
		if(!r) L[l]++;
	}
	for(int i = 1; i <= maxn; i++) ans += L[i] * R[i];
	ans += L[0] * R[0]; //自身就匹配的串的个数
	cout << ans << endl;
} 
### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值