A - XORwice
原题链接:https://codeforces.com/problemset/problem/1421/A
两个一样的数异或等于0.
找规律,根据样例显然可知x = a或者x = b的时候有最小值。
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll a, b, ans, x;
int main(){
cin >> t;
while(t--){
cin >> a >> b;
ans = (a ^ a) + (b ^ a);
cout << ans << endl;
}
C - Palindromifier
原题链接:https://codeforces.com/problemset/problem/1421/C
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
char sr[N];
int main(){
cin >> sr + 1;
int len = strlen(sr + 1);
int a = 1, b = len;
while(a < b){
if(sr[a] != sr[b]) break;
a++;
b--;
}
if(a >= b) printf("0\n");
else{
printf("3\n");
printf("R %d\n", len - 1);
printf("L %d\n", len);
printf("L 2\n");
}
return 0;
}
F - Andy’s First Dictionary
知识点:
1.tolower函数
作用:将大写字母变成小写
头文件:#include <stdlib.h>
定义函数:int tolower(int c);
函数说明:若参数 c 为大写字母则将该对应的小写字母返回。
返回值:返回转换后的小写字母,若不须转换则将参数c 值返回。
2.isalpha()函数(其实是一种宏定义)
作用:测试字符是否为英文字母
头文件:#include <ctype.h>
定义函数:int isalpha(int c)
函数说明:检查参数c是否为英文字母,在标准c中相当于使用“isupper©||islower©”做测试。
返回值:若参数c为英文字母,则返回TRUE,否则返回NULL(0)。
3. stringstream stringstream默认空格会直接分词看另一篇博客的5.例题:https://blog.youkuaiyun.com/LXC_007/article/details/113106996
4. set函数自带去重和按字典序大小储存
AC代码:
#include <bits/stdc++.h>
using namespace std;
set<string> diction;
int main(){
string a, b;
while(cin >> a){
for (int i = 0; i < a.length(); ++i){
if(isalpha(a[i])){
a[i] = tolower(a[i]);
}
else a[i] = ' ';
}
stringstream ss(a);
while(ss >> b)
diction.insert(b);
}
for (set<string>::iterator p = diction.begin(); p != diction.end(); ++p)
cout << *p << endl;
return 0;
}
H - 最大子矩阵
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1559
显然用前缀和做。
AC代码:
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#define jiechufengyin std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
const int N = 1e3 + 7;
ll t, m, n, x, y, ans = 0;
ll mmap[N][N];
int main(){
jiechufengyin;
cin >> t;
while(t--){
cin >> m >> n >> x >> y;
for(int i = 1; i <= m; ++i){
for (int j = 1; j <= n; ++j){
scanf("%lld", &mmap[i][j]);
mmap[i][j] = mmap[i-1][j] + mmap[i][j-1] - mmap[i-1][j-1] + mmap[i][j];
}
}
for(int i = 1;i <= m - x;i ++){
for(int j = 1;j <= n - y;j ++){
ans = max(ans , mmap[i + x - 1][j + y - 1] - mmap[i - 1][j + y - 1] - mmap[i + x - 1][j - 1] + mmap[i - 1][j - 1]);
}
}
cout << ans << endl;
}
return 0;
}
hdu卡cin、cout和scanf、printf卡的比较狠。。。所以上面这个代码在hdu里AC不了。
hduAC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 7;
ll t, m, n, x, y, ans = 0;
ll mmap[N][N];
int main(){
scanf("%lld", &t);
while(t--){
scanf("%lld%lld%lld%lld", &m, &n, &x, &y);
// cin >> m >> n >> x >> y;
for(int i = 1; i <= m; ++i){
for (int j = 1; j <= n; ++j){
scanf("%lld", &mmap[i][j]);
mmap[i][j] = mmap[i-1][j] + mmap[i][j-1] - mmap[i-1][j-1] + mmap[i][j];
}
}
for(int i = 1;i <= m - x;i ++){
for(int j = 1;j <= n - y;j ++){
ans = max(ans , mmap[i + x - 1][j + y - 1] - mmap[i - 1][j + y - 1] - mmap[i + x - 1][j - 1] + mmap[i - 1][j - 1]);
}
}
printf("%lld\n", ans);
}
return 0;
}
I - Ordering Tasks(拓扑排序 )
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 5;
int g[maxn][maxn];
int flag[maxn];
int ans[maxn];
int m, n, t;
bool dfs(int u) {
flag[u] = -1;
for(int v = 0; v < n; v++) {
if(g[u][v]) {
if(flag[v] < 0) return false;
else if(!flag[v]) dfs(v);
}
}
flag[u] = 1;
ans[--t] = u;
return true;
}
bool topoSort() {
t = n;
for(int i = 0; i < n; i++) {
if(!flag[i])
if(!dfs(i)) return false;
}
return true;
}
int main()
{
while(scanf("%d%d", &n, &m) == 2 && n ) {
memset(g, 0, sizeof(g));
for(int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b); a--; b--;
g[a][b] = 1;
}
memset(flag, 0, sizeof(flag));
if(topoSort()) {
for(int i = 0; i < n - 1; i++)
printf("%d ", ans[i] + 1);
printf("%d\n", ans[n-1] + 1);
}
else
printf("No\n");
}
return 0;
}