算法题代码汇总
以下是4道不同算法题的完整可直接复制运行的C++代码:
T1
problem
还原零一串(restore)
题目描述
小A最初有一个01串,长度为n,他执行了以下操作数次(可能没有进行操作):
- 将0添加到字符串的一端,并将1添加到字符串的另一端。例如,从字符串
1011开始,可以得到010111或110110。
不巧的是,小A忘了自己进行了几次操作,他只记得最终这个01串是什么。
小A想知道,他的原始字符串的最短长度是多少?
输入格式
从文件restore.in中读入数据。
输入第一行包含一个正整数T,表示测试用例的数量。
每个测试用例的第一行包含一个正整数n,表示这个01串最终的长度。
每个测试用例的第二行包含一个长度为n的字符串,表示这个01串最终是什么。
输出格式
输出到文件restore.out中。
对每个测试用例,输出一个非负整数,表示这个01串的原始字符串的最短长度,用换行隔开。
样例1输入
1
9
2 3 3 100
4 4 5 0111
6 5 7 10101
8 6 9 101010
10 7 11 1010110
12 1 13 1
14 2
15 10
16 2
17 11
18 10
19 1011011010数据范围
对于100%的数据,满足。
AC code
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
freopen("restore.in","r",stdin);
freopen("restore.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin>>T;
vector<int> ans;
while(T--){
int len;string s;
cin>>len>>s;
int l=0,r=len-1;
while(s[l]!=s[r] and l<=r) l++,r--;
ans.push_back(r-l+1);
}
for(int i:ans) cout<<i<<endl;
return 0;
}
/*
9
3
100
4
0111
5
10101
6
101010
7
1010110
1
1
2
10
2
11
10
1011011010*/
思路:双指针贪心WA code
无代码问题
没有问题
T2
problem
cyw 的学习计划(study)
题目描述
cyw 最近收到了一笔奖学金,于是她准备报一些课程来提升自己。
但是 cyw 想报的三门课开课时间是完全相同的 n 天,那么也就是说 cyw 每天只能选择其中一门课去上。
而对于第 i 天的课程,第一门课的价格为 ,第二门课的价格为 ,第三门课的价格为 。
同时 cyw 不希望连续两天上同样的课程,这会让她觉得无聊。
现在她想知道,在保证每天都上一门课的情况下,学完这 n 天最少需要花多少钱?
输入格式
从文件 study.in 中读入数据。
第一行只有一个整数 n,表示课程有 n 天。
第二行有 n 个正整数 ,表示第一门课每天的价格。
第三行有 n 个正整数 ,表示第二门课每天的价格。
第四行有 n 个正整数 ,表示第三门课每天的价格。
输出格式
输出到文件 study.out 中。
输出仅有一行,该行只有一个整数,表示最小花费。
样例 1 输入
5
1 3 1 2 2
1 2 3 4 3
4 2 1 5 3样例 1 输出
9数据范围
- 对于 30% 的数据,;
- 对于 60% 的数据,;
- 对于 100% 的数据,。
AC code
#include<bits/stdc++.h>
using namespace std;
#define int long long
int dp[4]={0};
int clas[4][100001]={0};
int n,dp1,dp2,dp3;
signed main(){
freopen("study.in","r",stdin);
freopen("study.out","w",stdout);
cin>>n;
for(int j=1;j<=3;j++)
for(int i=1;i<=n;i++){
cin>>clas[j][i];
}
for(int day=1;day<=n;day++){
dp1=min(dp[2],dp[3])+clas[1][day];
dp2=min(dp[1],dp[3])+clas[2][day];
dp3=min(dp[2],dp[1])+clas[3][day];
dp[1]=dp1;
dp[2]=dp2;
dp[3]=dp3;
}
cout<<min(dp1,min(dp2,dp3));
return 0;
}/*
5
1 3 1 2 2
1 2 3 4 3
4 2 1 5 3
*/
思路:DPWA code
无代码问题
无T3
problem
迷宫(maze)
题目描述
给你一个 n 行 n 列的包含 0,1 的矩阵,0 表示空地,1 表示障碍。起点为左上角 (1,1) 位置,终点为右下角 (n,n) 位置,每次只能向右走、向下走、向左走、向上走;假如当前在位置 ,你只能走到 、、、 位置。
每个位置每个单位时间都会变换状态,例如 在第 个单位时间是空地,则在第 个单位时间是障碍。 在第 个单位时间是障碍,则在第 个单位时间是空地。
保证起点的初始状态是空地。保证起点出发可以到达终点。
问起点到终点的最短时间。
注意: 1、行走过程中,不能在某个点上逗留。 2、不论自身所在位置是空地还是障碍,只要下一步到达的位置是空地,那么这个位置就能走。
输入格式
从文件 maze.in 中读入数据。
第一行一个正整数 n。
接下来 n 行,每行一个长度为 n 的整数序列,描述这个矩阵。
输出格式
输出到文件 maze.out 中。
一行一个数,表示答案。
样例 1 输入
3
000
101
010样例 1 输出
4样例 2 输入
3
000
100
010样例 2 输出
4数据范围
- 对于测试点 1~4:;
- 对于测试点 5~8:;
- 对于测试点 9~10:。
AC code
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct Node{
int x,y,t=0;
};
queue<Node> q;
int mp[1001][1001],n;
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
bool vis[1001][1001];
signed main(){
freopen("maze.in","r",stdin);
freopen("maze.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>mp[i][j];
if(n==1) cout<<0;
Node start;
start.t=0;
start.x=1;
start.y=1;
q.push(start);
vis[1][1]=1;
while(!q.empty()){
Node sc=q.front();
q.pop();
int x1,y1,t1;
Node n1;
for(int i=0;i<=3;i++){
x1=sc.x+dx[i];
y1=sc.y+dy[i];
t1=sc.t+1;
if(x1==n and y1==n and mp[x1][y1]==t1%2){
cout<<t1;
return 0;
}
if(x1>=1 and x1<=n and y1>=1 and y1<=n and mp[x1][y1]==t1%2 and vis[x1][y1]==0){
vis[x1][y1]=1;
n1.t=t1;
n1.x=x1;
n1.y=y1;
q.push(n1);
}
}
}
/*
3
0 0 0
1 0 0
0 1 0
*/
return 0;
}
思路:BFSWA code
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct Node{
int x,y,t=0;
};
queue<Node> q;
int mp[1001][1001],n;
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
bool vis[1001][1001];
signed main(){
freopen("maze.in","r",stdin);
freopen("maze.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>mp[i][j];
Node start;
start.t=0;
start.x=1;
start.y=1;
q.push(start);
vis[1][1]=1;
while(!q.empty()){
Node sc=q.front();
q.pop();
int x1,y1,t1;
Node n1;
for(int i=0;i<=3;i++){
x1=sc.x+dx[i];
y1=sc.y+dy[i];
t1=sc.t+1;
if(x1==n and y1==n and mp[x1][y1]==t1%2){
cout<<t1;
return 0;
}
if(x1>=1 and x1<=n and y1>=1 and y1<=n and mp[x1][y1]==t1%2 and vis[x1][y1]==0){
vis[x1][y1]=1;
n1.t=t1;
n1.x=x1;
n1.y=y1;
q.push(n1);
}
}
}
/*
3
0 0 0
1 0 0
0 1 0
*/
return 0;
}代码问题
忘记特判边长为一输出0
T4
problem
异或平方和(origen)
题目描述
给定 个整数 ,求
其中 表示按位异或运算。
注:。
输入格式
从文件 origen.in 中读入数据。
第一行,一个整数 。
第二行, 个整数,第 个整数表示 。
输出格式
输出到文件 origen.out 中。
一行一个整数,表示上式对 取模的结果。
样例 1 输入
3
1 2 3样例 1 输出
24样例 2 输入
6
1 1 4 5 1 4样例 2 输出
202数据范围
- 对于 的数据,;
- 对于 的数据,;
- 对于另外 的数据,;
- 对于 的数据,。
AC code
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MOD=998244353;
signed main(){
freopen("origen.in","r",stdin);
freopen("origen.out","w",stdout);
int n;
cin>>n;
vector<int> pre(n+1);
for(int i=1;i<=n;++i){
int x;
cin>>x;
pre[i]=pre[i-1]^x;
}
vector<int> pm(37,1);
for(int i=1;i<=36;++i){
pm[i]=pm[i-1]*2%MOD;
}
int ans=0;
for(int i=0;i<18;i++){
for(int j=0;j<18;j++){
int m=pm[i+j];
int num[3][3]={0};
num[0][0]=1;
for(int k=1;k<=n;k++){
int x=(pre[k]>>i)&1;
int y=(pre[k]>>j)&1;
ans=(ans+num[x^1][y^1]*m)%MOD;
num[x][y]++;
}
}
}
cout<<ans;
return 0;
}
思路:二进制乘法+前缀和WA code
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
freopen("origen.in","r",stdin);
freopen("origen.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int a[200001]={0};
int n;
cin>>n;
for(int i=1;i<=n;i++){cin>>a[i];a[i]^=a[i-1];}
int sum=0;
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
sum+=(a[j]^a[i-1])%998244353*(a[j]^a[i-1])%998244353;
sum%=998244353;
cout<<sum%998244353;
return 0;
}错误原因
不会推导公式