codeforces 862 AB

文章提供了两个编程问题的解决方案。第一个问题是关于数组异或操作,寻找一个数x使数组异或后结果为0。原始解法是遍历所有可能的x,而正确方法是计算数组的异或值,根据数组长度的奇偶性确定x。第二个问题涉及字符串操作,要求重新排列字符串以达到最小字典序,策略是找到第一个小于等于首字符的后续字符并交换位置。
摘要由CSDN通过智能技术生成

A:Problem - A - Codeforces (Unofficial mirror by Menci)

题意:给定一个数组a,让a中的数组异或同一个数x,得到b1,b2...bn,然后b1,b2..bn在异或 ,使得最后的结果为0。 

我的思路:因为x的范围为0到256,a数组的长度为0到1000,所以可以直接暴力,时间复杂度最大为O(256*1000),不会超时。还有0^n=n,n^n=0(这个不知道...)

下面为我的代码

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
using namespace std;
typedef  long  long ll ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
void print(__int128 num) {
	if (num) {
		print(num / 10);
		putchar(num % 10 + '0');
	}
}
int t;
int a[1005];
int main() {
	int n;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
		}
		int sum;
		for (int i = 0; i < 256; i++) {
			sum = 0;
			for (int j = 1; j <= n; j++) {
				sum = sum ^ a[j] ^ i;
			}
			if (sum == 0) {
				printf("%d\n", i);
				break;
			}
		}
		if (sum != 0) {
			printf("-1\n");
		}
	}


	return 0;
}

比较正确的思路:因为异或满足交换律和结合律。交换律:a1^a2=a2^a1;结合律:(a^b)^c=a^(b^c)

所以原题可以先将a1,a2..an异或再和n个x异或

情况有三种:设a数组异或为v

1.当v=0时,x为0;

2.当a数组的长度为奇数的时候,输出v,因为当a数组异或为v时候,x一定为v,n个x异或,如果n为奇数,异或为v,如果n为偶数异或为0;

3.当a数组长度为偶数时候,没有结果。

代码如下:

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
using namespace std;
typedef  long  long ll ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
void print(__int128 num) {
	if (num) {
		print(num / 10);
		putchar(num % 10 + '0');
	}
}
int a[1005];
int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n;
		scanf("%d", &n);
		int sum = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			sum = sum ^ a[i];
		}
		if (sum == 0) {
			printf("0\n");
		} else {
			if (n & 1) {
				printf("%d\n", sum);
			} else {
				printf("-1\n");
			}
		}
	}

	return 0;
}

B:Problem - B - Codeforces (Unofficial mirror by Menci)

题意:给定一个长度为n的字符串,你有唯一一次机会,将第2到第n个字符放在第一个位置,然后其他字符的位置不动,求最小的字典序。

我的思路:只有后面的字符小于等于第一个字符,才进行交换,而且交换的字符为最后一个小于小于等于第一个字符的字符,所以用pos来储存位置,pos的初值为0,注意字符串的长度可以为1和换行符号,注意!!!吃了大亏...

代码如下:

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
using namespace std;
typedef  long  long ll ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
void print(__int128 num) {
	if (num) {
		print(num / 10);
		putchar(num % 10 + '0');
	}
}
int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n;
		scanf("%d", &n);
		string s;
		cin >> s;
		int pos = 0;
		for (int i = 1; i <= n - 1; i++) {
			if (s[i] <= s[pos])pos = i ;
		}
		if (pos == 0) {
			cout << s;
			printf("\n");
			continue;
		} else {
			for (int i = 0; i <= n - 1; i++) {
				if (i == 0) {
					cout << s[pos];
				} else {
					if (i <= pos) {
						cout << s[i - 1];
					} else {
						cout << s[i];
					}
				}
			}
		}
		printf("\n");
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值