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;
}