Codeforces Round #659 (Div. 2) 部分题解

A . A.
题意: 给定一个长度为 n n 的序列 a a a i a_i 为串第 s i s_i s i + 1 s_{i+1} 的最长公共前缀长度,请你构造出一种合法方案输出 n + 1 n+1 个串。
数据范围: 0 a i 50 0 \leq a_i\leq 50 n 100 n\leq 100
题解: s i + 1 s_{i+1} 初始化为 s i s_i ,更改 s [ i + 1 ] [ q [ i ] ] s[i+1][q[i]] 即可(索引从 0 0 开始)
代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int q[N], n;
int main()
{
	int T; scanf("%d", &T);
	string s = "abcdefghijklmnopqrstuvwxyz";
	while(T--) {
		scanf("%d", &n);
		for(int i = 1; i <= n; i++) scanf("%d", &q[i]);
		cout << s << s << "\n";
		string temp = s + s;
		for(int i = 1; i <= n; i++) {
			string ts = temp;
			ts[q[i]] = (temp[q[i]] - 'a' + 1) % 26 + 'a';
			temp = ts;
			cout << ts << "\n";
		}
	}
	return 0; 
}

B . B. 鸽了,待补


C . C.
题意: 给定两个串 s 1 s_1 s 2 s_2 ,串长均为 n n ,串中字符只涉及到 a t a-t ,每个可以选择一种字符 c h 1 ch_1 ,挑选其中任意多个变成 c h 2 ch_2 ,至少多少次可以修改完成。
数据范围: n 1 0 5 n\leq 10^5
题解: 先记录下每个字符需要转换到的字符,然后遍历 19 19 次,每次将 i + a i+'a' 这个字符统一转换成 i + a i+'a' 需要转换到的 a s c i i ascii 码最低的字符。
代码:

#include<bits/stdc++.h>
using namespace std;

int id(char ch) {
	return ch - 'a';
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int T; cin >> T;
	while(T--) {
		vector<int> g[20];
		int n; cin >> n;
		string s1, s2;
		cin >> s1 >> s2;
		
		int flag = 1;
		for(int i = 0; i < n; ++i) {
			if(s1[i] > s2[i]) {
				flag = 0;
				break;
			}
			else if(s1[i] < s2[i]) {
				g[s1[i] - 'a'].push_back(s2[i] - 'a');
			}
		}
		
		if(flag) {
			int res = 0; 
			for(int i = 0; i < 19; ++i) {
				sort(g[i].begin(), g[i].end());
				g[i].erase(unique(g[i].begin(), g[i].end()), g[i].end());
				int len = g[i].size();
				res += (len > 0);
				for(int j = 1; j < len; j++) {
					if(g[i][j] == g[i][0]) continue;
					else g[g[i][0]].push_back(g[i][j]);
				}
			}
			cout << res << "\n";
		}
		else cout << -1 << "\n";
	}
	return 0;
}

D . D.
题意: K o a Koa K o a l a Koala 两人玩游戏,初始分均为 0 0 ,每次两人从数字序列中选择一个,选择后该数字就会被删除,两人的分数异或上该数字的值为新的分数,问均采取最优策略谁能赢, K o a Koa 先手。
题解: 从最高位开始判断,如果最高位数量模 4 4 1 1 ,则先手赢;若模 4 4 3 3 ,则总数字数为奇数必输,总数字为偶数必赢。否则判断次高位,依次类推,最低位数量模 4 4 后仍然为偶数则两者打平。
代码:

#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
int n, q[N];
int bit[32];

int dfs(int u) {
	if(u < 0) return 0;
	
	if(bit[u] % 4 == 1) return 1;
	if(bit[u] % 4 == 3) {
		if((n - bit[u]) & 1) return 1;
		return -1;
	}
	return dfs(u - 1);
}

int main()
{
	int T;
	scanf("%d",  &T);
	while(T--) {
		memset(bit, 0, sizeof bit);
		scanf("%d", &n);
		int mmax = 0;
		for(int i = 1; i <= n; i++) {
			scanf("%d", &q[i]);
			for(int j = 0; j < 32; j++) 
				if(q[i] >> j & 1) ++bit[j], mmax = max(mmax, j);
		}
		int t = dfs(mmax);
		if(t == 1) puts("WIN");
		else if(t == -1) puts("LOSE");
		else puts("DRAW");
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43900869/article/details/107572929