codeforces每日做题

Problem - D - Codeforces

其实就是找一个字符串,循环周期长度不是n的因数

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
const int N = 2e5+5;
int h[100005];
int n, k;
int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}

inline void solve(){
	int n; cin >> n;
	for (int i = 2; i <= n; i++) {
		if (n % i) {
			int cnt = 0;
			for (int j = 0; j < n; j++) {
				cout << (char)('a' + cnt);
				cnt++;
				cnt %= i;
			}
			cout << '\n';
			return;
		}
	}
	int cnt = 0;
	for (int i = 1; i <= n; i++) {
		cout << (char)('a' + cnt);
		cnt++;
		cnt %= n;
	}
	cout << '\n';
}
signed main(){

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

	int t = 1;
	cin >> t;
	while (t--)
		solve();

	return 0;
}

Problem - D - Codeforces

前缀和,后缀和还有特判

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
const int N = 2e5+5;
int h[100005];
int n, k;
int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}

inline void solve(){
	int n; cin >> n;
	string s; cin >> s;
	vector<int>q(n);
	vector<int>h(n);
	if (n & 1) {
		cout << -1 << '\n';
		return ;
	}
	int cntl = 0, cntr = 0;
	int cnt = 1;
	int fg1 = 0, fg2 = 0;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == '(') {
			cntl++;
		}
		else {
			cntr++;
		}
	}
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == '(') {
			fg1++;
		}
		else {
			fg1--;
		}
		if (fg1 < 0)
			break;
		if (fg1 == 0 && i == n - 1) {
			cout << 1 << '\n';
			for (int i = 0; i < s.size(); i++) {
				cout << 1 << ' ';
			}
			cout << '\n';
			return;
		}
	}
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == ')') {
			fg2++;
		}
		else {
			fg2--;
		}
		if (fg2 < 0)
			break;
		if (fg2 == 0 && i == n - 1) {
			cout << 1 << '\n';
			for (int i = 0; i < s.size(); i++) {
				cout << 1 << ' ';
			}
			cout << '\n';
			return;
		}
	}
	if (cntl != cntr) {
		cout << -1 << '\n';
		return;
	}
	vector<int>a(s.size());
	if (s[0] == '(')
		q[0] = 1;
	if (s[n - 1] == ')')
		h[n - 1] = 0;
	for (int i = 1; i < s.size(); i++) {
		q[i] = q[i - 1] + (s[i] == '(');
	}
	for (int i = n - 2; i >= 0; i--) {
		h[i] = h[i + 1] + (s[i] == ')');
	}
	for (int i = 0; i < n; i++) {
		if (q[i] == h[i+1]) {
			for (int j = 0; j <= i; j++)
				if(s[j]=='(')
				a[j] = 1;
			for (int j = n-1; j >= i+1; j--)
				if (s[j] == ')')
					a[j] = 1;
		}
	}
	cout << 2 << '\n';
	for (int j = 0; j < n; j++) {
		if (a[j] == 0)
			cout << 2 << ' ';
		else
			cout << 1 << ' ';
	}
	cout << '\n';
}
signed main(){

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

	int t = 1;
	cin >> t;
	while (t--)
		solve();

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值